c-加载PHP扩展模块失败


c - load PHP extension module failed

我想要的是构建一个*.so,然后将其作为PHP扩展模块,并通过PHP调用*.so中的函数。

我的步骤如下:

  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. 下载php5.2.17源代码

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

  3. cd php.5.2.17

  4. /配置/configure--prefix=/home/user1/php-5.2.17

  5. make&制作安装

  6. cd ext;

  7. /ext_skel--extname=你好

  8. cd你好

  9. 通过删除第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])

  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.17/ext/hello/module下生成hello.so,但是使用它打印的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()。。。。

顺便说一句,我把hello.so复制到了另一个LAMP环境中,而不是仅仅使用PHP构建。这两个版本都是5.2.17。

有人能指出发生了什么事吗?

在步骤15中,将LDFLAGS=lhello更改为LDFLAGS=hello.o,然后就可以工作了。我不知道*出了什么问题。所以。不管怎样,现在已经修好了。

我试过了——使用hello=DIR,DIR是什么?是libhello.so的路径还是php扩展名hello的路径?