首頁  >  文章  >  後端開發  >  在Linux下用C擴展PHP(打包成so)的方法

在Linux下用C擴展PHP(打包成so)的方法

WBOY
WBOY原創
2016-08-08 09:21:271251瀏覽

本文主要講一下在Linux下用打包C擴充功能.so檔案和Windows下的不同,詳細的程式碼和設定方案請參加另一篇部落格:http://blog.csdn.net/maverick1990/article/details /46519045

步驟:

1.安裝php環境到目錄/usr/local/php/ 目錄下

2.下載相同版本的php來源碼包,安裝到/root/php-5.6.9/ 目錄下,可到官網http://www.php.net/downloads.php下載

執行指令:

cd /root
wget http://us1.php.net/distributions/php-5.6.9.tar.bz2
tar -xf php-5.6.9.tar.bz2
注意,有一些歷史版本沒有源碼包,需要升級php到提供源碼包的版本

3.到/php-5.6.9/ext/ 目錄下,使用ext_skel生成擴展骨架

cd ./php-5.6.9/ext
./ext_skel --extname=test

4.修改配置文件 /test/config.m4

取消下面兩行的dnl註釋:

PHP_ARG_ENABLE(test, whether to enable test support,
dnl Make sure that the comment is aligned:
[  --enable-test           Enable test support])

如果要使用C++進行編譯,要將test.c改名為test.cpp,並在config.m4中加入
PHP_REQUIRE_CXX()    
PHP_ADD_LIBRARY(stdc++, 1, EXTRA_LDFLAGS)
PHP_NEW_EXTENSION(test, test.cpp, $ext_shared)

5.在php_test.h檔案中加入程式碼,加入自訂的函數宣告:
PHP_MINIT_FUNCTION(test);
PHP_MSHUTDOWN_FUNCTION(test);
PHP_RINIT_FUNCTION(test);
PHP_RSHUTDOWN_FUNCTION(test);
PHP_MINFO_FUNCTION(test);

PHP_FUNCTION(confirm_test_compiled);	/* For testing, remove later. */
PHP_FUNCTION(testFunc);

6.在test.c/cpp中加入自訂函數程式碼:

(1)首先,在這個位置引入用到的頭檔:

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <string.h>
#include <math.h>
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_test.h"
否則,會出現很多函數重定義的問題,詳見另一部落格:http://blog.csdn.net/maverick1990/article/details/46786685

(2)然後,在這個位置加入函數入口:

const zend_function_entry test_functions[] = {
	PHP_FE(confirm_test_compiled, NULL)		/* For testing, remove later. */
	PHP_FE(testFunc, NULL)
	PHP_FE_END	/* Must be the last line in test_functions[] */
};

(3)最後,在檔案尾部加入testFunc函數的程式碼,也可以在這裡定義其他自訂函數
PHP_FUNCTION(testFunc)  
{  
    char *x = NULL;  
    char *y = NULL;  
    int argc = ZEND_NUM_ARGS();  
    int x_len;  
    int y_len;  
  
    if (zend_parse_parameters(argc TSRMLS_CC, "ss", &x, &x_len, &y, &y_len) == FAILURE)   
        return;  
      
    int result_length = x_len + y_len;  
    char* result = (char *) emalloc(result_length + 1);  
    strcpy(result, x);  
    strcat(result, y);  
  
    RETURN_STRINGL(result, result_length, 0);  
}  

關於PHP_FUNCTION函數的書寫方法,請參閱部落格:http://weizhifeng.net/write-php-extension-part2-1.html

7.在/root/php-5.6.9/ext/ test/ 目錄下,建立php擴充模組:

phpize
可能需要加上路徑:
/usr/local/php/bin/phpize

8.回到 /root/php-5.6.9/ 目錄,重新建立編譯所需的配置:

cd ../..
./buildconf --force

若出現類似的報錯:

buildconf: You need autoconf 2.59 or lower to build this version of PHP.
          You are currently trying to use 2.63
          Most distros have separate autoconf 2.13 or 2.59 packages.
          On Debian/Ubuntu both autoconf2.13 and autoconf2.59 packages exist.
          Install autoconf2.13 and set the PHP_AUTOCONF env var to
          autoconf2.13 and try again.

那麼,透過下面的方法解決:
yum install autoconf213
export PHP_AUTOC/bin/autoconf-2.13
9.再到 /root/php-5.6.9/ext/test/ 目錄下,產生設定:
./configure --with-php-c/local/php/bin/php-config

10.編譯並產生.so擴充檔
make
make install
若make出錯,修改程式碼直到編譯通過

若make install出錯,修改後,需要清空目前產生的擴充模組:

phpize --clean

然後從第7步重新開始

11.查看test.so是否產生

make install成功後會給出生成路徑,例如:/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/test.so

12.修改php.ini

修改 /usr/local/php/etc/php.ini 文件,加入擴充路徑與名稱:

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/"

extension = "teste.so"

13.重啟PHP服務
service php-fpm restart

14.在PHP中使用C++擴充函數中使用C++擴充函數

<?php 
$concat_str = testFunc("concat1","concat2");
echo $concat_str;
?>

注意:若使用擴充函數時出現錯誤:
PHP Warning  PHP Startup Unable to initialize module 
Module compiled with module API=20121212 PHP    
compiled with module API=20090626 
These options need to match  in Unknown on line 0

這是由於編譯.so檔案的php源碼包版本和目前使用的php版本不同所致,需要在下載當前版本的php源碼包,重新編譯擴展

版權聲明:本文為博主原創文章,未經博主允許不得轉載。

以上就介紹了在Linux下用C擴展PHP(打包成so)的方法,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn