用apache的动态so模式编写和编译php扩展

看过我另外一篇关于用c语言编写php扩展so模块的朋友,应该已经了解大致的php模块编写过程,(原文:http://www.toplee.com/blog/56.html) 不过该文中没有专门针对已经安装好的系统进行说明,如何在现有apache、php运行环境下添加一个动态的so扩展,下面简单的整理一下。

大致的步骤如下:
[coolcode]
#cd /usr/port/distfiles/php-5.1.4/ext/ (进入到您的php源码目录)
#./ext_skel –extname=toplee (此处的toplee为您的扩展so的名称)
#cd ..
#vim ./ext/toplee/config.m4 (修改config.m4文件,此处细节可以参考我另外那篇文章中说明)
#vim ./ext/toplee/toplee.c (修改和加入您自己的c代码)
#vim ./ext/toplee/toplee.h (修改和添加函数定义代码)
#./buildconf –force (加上force参数是避免您使用的php版本为release版本)
如果上面命令执行中提示autoconf not found之类的错误,可以执行下面命令
#export PHP_AUTOCONF=/usr/local/bin/autoconf259 (这里的autoconf259路径和名称根据您自己安装的autoconf情况而定)
#export PHP_AUTOHEADER=/usr/local/bin/autoheader259 (autoheader259同上)
#./buildconf –force
#./configure –disable-all –with-toplee=shared –with-apxs2=/usr/local/sbin/apxs
上面命令中,–disable-all是为了加快编译速度而使用的,减少php默认要编译的模块数量。 –with-toplee=shared为了编译后能直接生产.so文件, –with-apxs2=/usr/local/sbin/apxs是根据您服务器上apache安装具体路径和版本来确定的
#make
#cp ./ext/toplee/.libs/toplee.so /usr/local/lib/php/20050922/ (把生产的so文件拷贝到您的php存放so扩展文件的目录下)
#vim /usr/local/etc/php/extensions.ini (加入toplee.so一行)
#/usr/local/etc/rc.d/apache22.sh restart
[/coolcode]
最后从phpinfo()函数看看返回结果,如果看到有toplee部分,那么恭喜你!如果没有,按照步骤好好检查一下!
这是我的phpinfo看到的结果:
topleeso

另外,顺便提一下另外一种编译php扩展的方法,就是使用phpize工具,这种方法允许你在一个独立编写的目录下编译php的扩展,不用按照前面的方式在php源文件目录的ext下,大致的思路是:
1.创建自己的扩展目录 toplee
2.编写config.m4文件,可以参考前面ext_skel生成的范本
3.执行 phpize 命令
4.执行 ./configure –enable-toplee –with-apxs2=/usr/local/sbin/apxs
–with-php-config=/usr/local/bin/php-config
5.make

这时会在当前目录下生产一个toplee.so,呵呵,这就是你想要的!

Category: PHP / Zend  Tags: , , ,
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
2 Responses
  1. zabke says:

    这个theme很漂亮啊

  2. PeterFu says:

    谢谢!
    编译自己的扩展很有意思。

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image