megnlingling 发表于 2015-8-28 09:59:46

PHP扩展编写与编译

  研究PHP扩展怎么写来着,但首先的过编译这关,下面介绍下
  假如PHP安装在,且安装了phpize
  /usr/local/php
  源码包在
  /root/lamp/php-5.3.2
  下面先以编译官网提供的代码为例:
  随便进入一个扩展,我进入如下:
  /root/lamp/php-5.3.2/ext/soap
  先生成PHP的编译配置:
  /usr/local/php/bin/phpize
  生成相应的config.h文件,并编译
  ./configure --with-php-config=/usr/local/php/bin/php-config
  make;make install
  到最后会提示安装到了那个目录
  Installing shared extensions:   /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
  ll /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
  发现多了个soap.so文件
  在玩php.ini 加上
  extension=soap.so
  如果是以模块运行,重启PHP,完毕
  下面是介绍编写PHP扩展
  进入/root/lamp/php-5.3.2/ext/
  生成一个扩展
  ./ext_skel --extname=myext
  进入/myext
  选择扩展类型:
  vi config.m4
  下面两种类型选一个就行了

  (依赖外部库)

  dnl PHP_ARG_WITH(myext, for myext support,
  dnl Make sure that the comment is aligned:
  dnl [ --with-myext Include myext support])
  去掉dnl
  或者将
  

(不依赖外部库)   
  dnl PHP_ARG_ENABLE(myext, whether to enable myext support,
  dnl Make sure that the comment is aligned:
  dnl [ --enable-myext Enable myext support])
  去掉dnl
  修改头文件php_myext.h
  将
  PHP_FUNCTION(mytest); /* For testing, remove later. */
  更改为
  PHP_FUNCTION(myext);
  修该扩展C文件myext.c
  zend_function_entry php5cpp_functions[] = {
  PHP_FE(confirm_myext_compiled, NULL) /* For testing, remove later. */
  {NULL, NULL, NULL} /* Must be the last line in php5cpp_functions[] */
  };
  更改为:
  zend_function_entry php5cpp_functions[] = {
  PHP_FE(myext, NULL) /* For testing, remove later. */
  {NULL, NULL, NULL} /* Must be the last line in php5cpp_functions[] */
  };
  在该文件的最下面编写你的myext函数
  PHP_FUNCTION(myext)
  {
  zend_printf("hello world\n");
  }
  保存,按上述方法编译,并修改php.ini
  vi test.php
  <?php
  myext();
  运行测试:
  /usr/local/php/bin/php test.php
  输出:hello,world
  完毕
  不熟C语言,C高手可以自己编写属于自己的扩展,毕竟运行速度比PHP脚本块M多
  我编译出来的,给个图:下载编译出来的so文件

  

  流程图:
  

  
页: [1]
查看完整版本: PHP扩展编写与编译