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

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] <html> <body> <?php echo "Hello World"; phpinfo(); ?> </body> </html> [/html]
4. 如果已经安装了两个版本的php,使用php查看版本号或确定php.ini的配置文件 #/usr/local/php/bin/php -v #/usr/local/php/bin/php -i | grep php.ini

第二步: 用C生成so文件

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

第三步: 制作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] 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[] */}; 然后在 hello.c 的最末尾书写hello_add函数的内容: 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 = cc_add(a, b); RETURN_LONG(result); } [/php]
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] <html> <body> <?php dl("hello.so"); echo hello_add(4, 6); ?> </body> </html> [/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] 代码如下: <html> <body> <?php echo hello_add(4, 6); ?> </body> </html> [/html]
原文地址:https://www.cnblogs.com/doseoer/p/4007632.html