php调用c语言编写的so动态库

来源:互联网 发布:装饰定额预算软件 编辑:程序博客网 时间:2024/05/16 07:42

from http://blog.csdn.net/wzhwho/article/details/6949297


PHP除了使用扩展库的方式调用c函数,还可以通过socket通信的方式。这里介绍前者。 

第一步: 环境搭建

1. 先看本机是否已经安装了较低版本的php
   #find /usr -name "php"
   或者rpm -aq | grep php
   如果存在,就使用rpm命令等方式卸掉
2. php源码安装,configure的参数如下
#./configure  --prefix=/usr/local/php   --with-mysql --with-mysqli  --enable-so  --with-pdo-mysq=/var/lib/mysql  --with-apxs2=/usr/local/apache/bin/apxs --with-zlib --enable-zip --enable-mbstring
3. 如果已经安装了两个版本的php,使用apache函数,确定使用的是哪个版本的php
[html] view plaincopy
  1. <html>  
  2. <body>  
  3. <?php  
  4. echo "Hello World";  
  5. phpinfo();  
  6. ?>  
  7. </body>  
  8. </html>  
4. 如果已经安装了两个版本的php,使用php查看版本号或确定php.ini的配置文件 
#/usr/local/php/bin/php -v
#/usr/local/php/bin/php -i | grep php.ini

第二步: 用C生成so文件

[cpp] view plaincopy
  1. #vim hello.c  
  2. int cc_add(int a, int b)  
  3. {  
  4.     return a + b;  
  5. }  
  6. # gcc -O -c -fPIC -o hello.o hello.c                      // -fPIC:是指生成的动态库与位置无关  
  7. # gcc -shared -o libhello.so hello.o                     // -shared:是指明生成动态链接库  
  8. # cp libhello.so /usr/local/lib      // 把生成的链接库放到指定的地址  
  9. # echo /usr/local/lib > /etc/ld.so.conf.d/local.conf       //  把库地址写入到配置文件中  
  10. # /sbin/ldconfig                // 用此命令,使刚才写的配置文件生效  
以下是测试so文件
[cpp] view plaincopy
  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int a = 4, b = 6;  
  5.     printf("%d\n", cc_add(a,b));  
  6.     return 0;  
  7. }  
  8. #gcc -o hellotest -lhello hellotest.c                // 编译测试文件,生成测试程序  
  9. #./hellotest           // 运行测试程序  

第三步: 制作PHP模块(外部模块)

1. 然后通过下面的命令用ext_skel脚本建立一个名为 hello 的模块:
#cd php-5.3.6/ext
#./ext_skel --extname=hello

2. 执行该命令之后它会提示你应当用什么命令来编译模块,可惜那是将模块集成到php内部的编译方法。
如果要编译成可动态加载的 php_hello.so,方法要更为简单。
#cd hello
首先编辑 config.m4 文件,去掉注释(注释符号为 dnl 。)
# vim config.m4
PHP_ARG_ENABLE(hello, whether to enable hello support,
dnl Make sure that the comment is aligned:
[  --enable-hello           Enable hello support])

3. 然后执行 phpize 程序,生成configure脚本:
#phpize

4. 打开 php_hello.h,在 PHP_FUNCTION(confirm_hello_compiled); 之下加入函数声明:
PHP_FUNCTION(confirm_hello_compiled);   /* For testing, remove later. */
PHP_FUNCTION(hello_add);

5. 打开 hello.c,在 PHP_FE(confirm_hello_compiled, NULL) 下方加入以下内容。
[php] view plaincopy
  1. zend_function_entry hello_functions[] = {  
  2. PHP_FE(confirm_hello_compiled,  NULL)       /* For testing, remove later. */  
  3. PHP_FE(hello_add,   NULL)       /* For testing, remove later. */  
  4. {NULL, NULL, NULL}  /* Must be the last line in hello_functions[] */};  
  5.   
  6. 然后在 hello.c 的最末尾书写hello_add函数的内容:  
  7. PHP_FUNCTION(hello_add)  
  8. {  
  9.     long int a, b;  
  10.     long int result;  
  11.   
  12.     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &a, &b) == FAILURE) {  
  13.         return;  
  14.     }  
  15.     result = cc_add(a, b);  
  16.   
  17.     RETURN_LONG(result);  
  18. }  
6.配置安装 
#./configure --with-php-config=/usr/local/php/bin/php-config 
#make LDFLAGS=-lhello
#make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
#cp modules/hello.so /usr/lib/php/modules

#vim /etc/php5/apache2/php.ini

enable_dl = Off; //允许dl()动态加载so扩展功能enable_dl = On

# /etc/init.d/httpd restart
然后在 /var/www/html 下建立一个 hello.php 文件,内容如下:
[html] view plaincopy
  1. <html>  
  2. <body>  
  3. <?php  
  4.     dl("hello.so");  
  5.     echo hello_add(4, 6);  
  6. ?>  
  7. </body>  
  8. </html>  
然后在浏览器中打开hello.php文件,如果显示10,则说明函数调用成功了。

第四步. 制作PHP模块(内部模块)

另外可以在apache重启的时候让我们的so库直接动态编译进php5,就像linux的insmod hello.ko模块一样,不用dl加载也不用重新编译php,就可以直接使用so的函数了,步骤如下:

# vim /etc/php5/apache2/php.ini
enable_dl = Off
extension=hello.so
# /etc/init.d/httpd restart

[注意,这种方式只适合hello.so库内所有功能代码已经全部调试ok,如果还处在调试期间,那么需要采用上面的dl强制加载的方式]

[html] view plaincopy
  1. 代码如下:  
  2. <html>  
  3. <body>  
  4. <?php  
  5.     echo hello_add(4, 6);  
  6. ?>  
  7. </body>  
  8. </html>  

0 0
原创粉丝点击