2014-09-28 136 views
1

我想要的是構建* .so,然後將其作爲PHP擴展模塊並通過PHP調用* .so中的函數。加載PHP擴展模塊失敗

我的步驟是如下:

  1. 建立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 
  1. 下載PHP 5.2.17源代碼

  2. 焦油-zxvf php.5.2.17.tar.gz

  3. CD php.5.2.17

  4. 的./configure的./configure - PREFIX = /家庭/用戶1/PHP-5.2.17

  5. 使&使安裝

  6. CD轉;

  7. ./ext_skel --extname =你好

  8. CD你好通過去除線16-18的DNL然後保存並退出

  9. 編輯的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])

  10. 執行命令:/home/user1/php-5.2.17/bin/phpize

  11. 開放php_hello.h,加 PHP_FUNCTION(hello_add);

  12. 開放的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); }

  13. ./configure --with-php-config=/home/usr1/php-5.2.17/bin/php-config

  14. make LDFLAGS=-lhello

  15. 然後被下/home/usre/php-5.2產生hello.so。17/EXT /你好/模塊,但 使用nm hello.so它打印:

      `U hello_add 
          0000000000000a98 T _init` 
    
  16. 創建一個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。

任何人都可以指出發生了什麼?

+0

你應該做'./configure ... --enable-hello';另外,如果你要包裝一個庫,你應該使用'--with-hello = DIR'語法。 – 2014-09-28 12:01:14

+0

你的意思是哪一步?步驟5或步驟14? – 2014-09-28 12:06:24

+0

在步驟14,配置您的分機。 – 2014-09-28 12:09:38

回答

0

我試過--with-hello = DIR,什麼是DIR?是libhello.so或PHP擴展的路徑 - 你好的路徑?

1

在步驟15中,將LDFLAGS = lhello更改爲LDFLAGS = hello.o,然後運行。我不知道* .so有什麼問題。無論如何,它現在是固定的。