手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux下C语言对PHP扩展

[日期:2010-02-07] 来源:Linux社区  作者:Linux [字体:大 中 小]

  一,搭建php环境

  下载php 5.2.6 源码 并解压

  编译安装,搭建php环境

  二,创建扩展项目

  进入源码目录

  cd php5.2.6/ext/

  ./ext_skel --extname=my_ext

  创建名字为my_ext的项目,最终会生成my_ext.so

  三,更改配置和程序

  $ vi ext/my_ext/config.m4

  根据你自己的选择将

  dnl PHP_ARG_WITH(my_ext, for my_ext support,

  dnl Make sure that the comment is aligned:

  dnl [ --with-my_ext Include my_ext support])

  修改成

  PHP_ARG_WITH(my_ext, for my_ext support,

  Make sure that the comment is aligned:

  [ --with-my_ext Include my_ext support])

  或者将

  dnl PHP_ARG_ENABLE(my_ext, whether to enable my_ext support,

  dnl Make sure that the comment is aligned:

  dnl [ --enable-my_ext Enable my_ext support])

  修改成

  PHP_ARG_ENABLE(my_ext, whether to enable my_ext support,

  Make sure that the comment is aligned:

  [ --enable-my_ext Enable my_ext support])

  $ vi ext/my_ext/php_my_ext.h

  将

  PHP_FUNCTION(confirm_my_ext_compiled); /* For testing, remove later. */

  更改为

  PHP_FUNCTION(say_hello);

  $ vi ext/my_ext/my_ext.c

  将

  zend_function_entry php5cpp_functions[] = {

  PHP_FE(confirm_my_ext_compiled, NULL) /* For testing, remove later. */

  {NULL, NULL, NULL} /* Must be the last line in php5cpp_functions[] */

  };

  更改为

  zend_function_entry php5cpp_functions[] = {

  PHP_FE(say_hello, NULL)

  {NULL, NULL, NULL} /* Must be the last line in php5cpp_functions[] */

  };

  在最后添加:

  PHP_FUNCTION(say_hello)

  {

  zend_printf("hello world\n");

  }

  四,编译

  $ cd my_ext

  $ /usr/local/php/bin/phpize

  ps: 如果出现:Cannot find autoconf.……的错误信息,则需要安装 autoconf (安装过程略)

  $ ./configure --with-php-config=/usr/local/php/bin/php-config

  $ make

  这时会编译出 my_ext/modules/my_ext.so

  五,配置php.ini

  将my_ext.so放入/usr/local/php/ext/目录

  $ vi php.ini

  修改添加如下:

  extension_dir = '/usr/local/php/ext/'

  extension=my_ext.so

  六,测试

  $ vi test.php

  <?php

  say_hello();

  ?>

  $ /usr/local/php/bin/php test.php

  hello world.

  则大功告成

linux
相关资讯       C语言 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款