Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6537811
  • 博文数量: 1159
  • 博客积分: 12444
  • 博客等级: 上将
  • 技术积分: 12570
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 21:34
文章分类

全部博文(1159)

文章存档

2016年(126)

2015年(350)

2014年(56)

2013年(91)

2012年(182)

2011年(193)

2010年(138)

2009年(23)

分类: PHP

2015-11-04 18:03:44

http://blog.csdn.net/maverick1990/article/details/46777905


本文主要讲一下在Linux下用打包C扩展程序.so文件和Windows下的不同,详细的代码和配置方案请参加另一篇博客:http://blog.csdn.net/maverick1990/article/details/46519045


步骤:

1.安装php环境到目录 /usr/local/php/ 目录下


2.下载相同版本的php源码包,安装到 /root/php-5.6.9/ 目录下,可到官网下载

执行命令:

  1. cd /root  
  2. wget http://us1.php.net/distributions/php-5.6.9.tar.bz2  
  3. tar -xf php-5.6.9.tar.bz2  
注意,有一些历史版本没有源码包,需要升级php到提供源码包的版本


3.到 /php-5.6.9/ext/ 目录下,使用ext_skel生成扩展骨架

  1. cd ./php-5.6.9/ext  
  2. ./ext_skel --extname=test  



4.修改配置文件 /test/config.m4

取消下面两行的dnl注释:

  1. PHP_ARG_ENABLE(test, whether to enable test support,  
  2. dnl Make sure that the comment is aligned:  
  3. [  --enable-test           Enable test support])  

如果要使用C++进行编译,要将test.c改名为test.cpp,并在config.m4中添加
  1. PHP_REQUIRE_CXX()      
  2. PHP_ADD_LIBRARY(stdc++, 1, EXTRA_LDFLAGS)  
  3. PHP_NEW_EXTENSION(test, test.cpp, $ext_shared)  



5.在php_test.h文件中添加代码,加入自定义的函数声明:
  1. PHP_MINIT_FUNCTION(test);  
  2. PHP_MSHUTDOWN_FUNCTION(test);  
  3. PHP_RINIT_FUNCTION(test);  
  4. PHP_RSHUTDOWN_FUNCTION(test);  
  5. PHP_MINFO_FUNCTION(test);  
  6.   
  7. PHP_FUNCTION(confirm_test_compiled);    /* For testing, remove later. */  
  8. PHP_FUNCTION(testFunc);  



6.在test.c/cpp中添加自定义函数代码:

(1)首先,在这个位置引入用到的头文件:

  1. #ifdef HAVE_CONFIG_H  
  2. #include "config.h"  
  3. #endif  
  4.   
  5. #include   
  6. #include   
  7. #include   
  1. #include "php.h"  
  2. #include "php_ini.h"  
  3. #include "ext/standard/info.h"  
  4. #include "php_test.h"  
否则,会出现很多函数重定义的问题,详见另一篇博客:http://blog.csdn.net/maverick1990/article/details/46786685

(2)然后,在这个位置添加函数入口:

  1. const zend_function_entry test_functions[] = {  
  2.     PHP_FE(confirm_test_compiled, NULL)     /* For testing, remove later. */  
  3.     PHP_FE(testFunc, NULL)  
  4.     PHP_FE_END  /* Must be the last line in test_functions[] */  
  5. };  

(3)最后,在文件尾部添加testFunc函数的代码,也可以在这里定义其他自定义函数
  1. PHP_FUNCTION(testFunc)    
  2. {    
  3.     char *x = NULL;    
  4.     char *y = NULL;    
  5.     int argc = ZEND_NUM_ARGS();    
  6.     int x_len;    
  7.     int y_len;    
  8.     
  9.     if (zend_parse_parameters(argc TSRMLS_CC, "ss", &x, &x_len, &y, &y_len) == FAILURE)     
  10.         return;    
  11.         
  12.     int result_length = x_len + y_len;    
  13.     char* result = (char *) emalloc(result_length + 1);    
  14.     strcpy(result, x);    
  15.     strcat(result, y);    
  16.     
  17.     RETURN_STRINGL(result, result_length, 0);    
  18. }    

关于PHP_FUNCTION函数的书写方法,详见博客:

7.在 /root/php-5.6.9/ext/test/ 目录下,建立php扩展模块

  1. phpize  
可能需要加上路径:
  1. /usr/local/php/bin/phpize  


8.回到 /root/php-5.6.9/ 目录,重新建立编译需要的配置:

  1. cd ../..  
  2. ./buildconf --force  

若出现类似的报错:

  1. buildconf: You need autoconf 2.59 or lower to build this version of PHP.  
  2.           You are currently trying to use 2.63  
  3.           Most distros have separate autoconf 2.13 or 2.59 packages.  
  4.           On Debian/Ubuntu both autoconf2.13 and autoconf2.59 packages exist.  
  5.           Install autoconf2.13 and set the PHP_AUTOCONF env var to  
  6.           autoconf2.13 and try again.  

那么,通过下面的方法解决:
  1. yum install autoconf213  
  2. export PHP_AUTOCONF=/usr/bin/autoconf-2.13  


9.再到 /root/php-5.6.9/ext/test/ 目录下,生成配置:
  1. ./configure --with-php-config=/usr/local/php/bin/php-config  

10.编译并生成.so扩展文件
  1. make  
  2. make install  
若make出错,修改代码直到编译通过

若make install出错,修改后,需要清空当前生成的扩展模块:

  1. phpize --clean  

然后从第7步重新开始


11.查看test.so是否生成

make install成功后会给出生成路径,例如:/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/test.so


12.修改php.ini

修改 /usr/local/php/etc/php.ini 文件,加入扩展路径和名称:

  1. extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/"  
  2.   
  3. extension = "teste.so"  

13.重启PHP服务
  1. service php-fpm restart  

14.在PHP中使用C++扩展函数

  1. 阅读(1363) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~