最近做一個語義機器人的項目,語義相似度比對算法不希望被客戶看到,需要用C++實作,PHP網頁裡面要調用。
我們建立擴充放在php的源碼包的ext目錄下。如/opt/php-5.3.2/ext/下
1) ext_skel來建立一個php擴充的一個架構
[plain]
[email protected]:/# cd /opt/php-5.3.2/ext/
ro[email protected]:opt/php-5.3.2/ext/]# ./ext_skel --extname=hello_module
在這個目錄下生成一個目錄叫hello_module
[plain]
[email protected]:opt/php-5.3.2/ext# #cd hello_module
[email protected]:/opt/php-5.3.2/ext/hello_module# ls
config.m4
config.w32
CREDITS
EXPERIMENTAL
hello_module.c
hello_module.php
php_hello_module.h
tests
2)編輯config.m4檔案,我們使用enable-hello_module選項:
dnl PHP_ARG_ENABLE(hello_module, whether to enablehello_module support,
dnl Make sure that the comment is aligned:
dnl [ --enable-hello_module Enable hello_module support])
修改成
PHP_ARG_ENABLE(hello_module, whether to enable hello_module support,
[ --enable-hello_module Enable hello_module support])
3)vi hello_module.c
主要是給子產品添加函數:
function_entry hello_module_functions[] = {
PHP_FE(hello_world, NULL)
PHP_FE(confirm_my_module_compiled, NULL)
{NULL, NULL, NULL}
};
在檔案的最後添加下列代碼
定義函數:
PHP_FUNCTION(hello_world)
{
zend_printf("hello sdomain!");
}
4)修改php_hello_module.h
在PHP_FUNCTION(confirm_my_module_compiled ); 這行的下面添加一行:
PHP_FUNCTION(hello_world);
5) 執行phpize并編譯
[email protected]:/opt/php-5.3.2/ext/hello_module# /usr/local/php/bin/phpize
執行以後會看到下面的
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
然後執行:
[email protected]:/opt/php-5.3.2/ext/hello_module#./configure --enable-hello_module --with-php-config=/usr/local/php/bin/php-config
[email protected]:/opt/php-5.3.2/ext/hello_module# make
這個時候會在目前的目錄下生成一個目錄叫modules先存放着hello_module.so檔案
cp modules/hello_module.so /usr/local/php/ext/
然後再把hello_module.so檔案拷到php.ini裡面的extension_dir所指定的位置6)開啟擴充
6)開啟擴充
php.ini檔案中打開這個擴充
extension=hello_module.so
重新起動apache
用phpinfo來察看一下ok了