我想要的是構建* .so,然後將其作爲PHP擴展模塊並通過PHP調用* .so中的函數。加載PHP擴展模塊失敗
我的步驟是如下:
建立linux下的C庫,首先創建的hello.c
int hello_add(int a, int b) { return a+b; }
然後建立如下:
gcc -O -c -fPIC -o hello.o hello.c
gcc -shared -o libhello.so hello.o
下載PHP 5.2.17源代碼
焦油-zxvf php.5.2.17.tar.gz
CD php.5.2.17
的./configure的./configure - PREFIX = /家庭/用戶1/PHP-5.2.17
使&使安裝
CD轉;
./ext_skel --extname =你好
CD你好通過去除線16-18的DNL然後保存並退出
編輯的config.m4。
16: PHP_ARG_ENABLE(hello, whether to enable hello support, 17: dnl Make sure that the comment is aligned: 18: [ --enable-hello Enable hello support])
執行命令:/home/user1/php-5.2.17/bin/phpize
開放php_hello.h,加
PHP_FUNCTION(hello_add);
開放的hello.c更改爲:
zend_function_entry hello_functions[] = { PHP_FE(confirm_hello_compiled, NULL) /* For testing, remove later. */ PHP_FE(hello_add, NULL) /* For testing, remove later. */ {NULL, NULL, NULL} /* Must be the last line in hello_functions[] */ };
在該文件的末尾,添加
PHP_FUNCTION(hello_add) { long int a, b; long int result; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &a, &b) == FAILURE) { return; } result = hello_add(a, b); RETURN_LONG(result); }
./configure --with-php-config=/home/usr1/php-5.2.17/bin/php-config
make LDFLAGS=-lhello
然後被下/home/usre/php-5.2產生hello.so。17/EXT /你好/模塊,但 使用
nm hello.so
它打印:`U hello_add 0000000000000a98 T _init`
創建一個PHP文件來測試:
<?php if(!dl('btest.so')) { echo "can not load hello.so"; } else { echo "load is done"; echo hello_add(3,4);// here it will throw error in the log } ?>
,它抱怨: [28-Sep-2014 18:38:28] PHP致命錯誤:調用未定義函數hello_add()...
BTW,I c將hello.so轉化爲另一個LAMP環境,而不是使用PHP構建。這兩個版本是5.2.17。
任何人都可以指出發生了什麼?
你應該做'./configure ... --enable-hello';另外,如果你要包裝一個庫,你應該使用'--with-hello = DIR'語法。 – 2014-09-28 12:01:14
你的意思是哪一步?步驟5或步驟14? – 2014-09-28 12:06:24
在步驟14,配置您的分機。 – 2014-09-28 12:09:38