ubuntu 下第一个php扩展开发记录

今天尝试了我的第一个php扩展开发,记录下过程以及遇到的问题


一、环境准备

以前我已经用以下命令安装过php了

$ sudo apt-get install php5

其安装位置是

$ whereis php
php: /usr/bin/php /usr/lib/php /usr/bin/X11/php /usr/share/man/man1/php.1.gz

这种方式安装的php并不能直接进行php扩展开发,我们还需要

(1)安装php5-dev,不然没有编译扩展需要的phpize

$ sudo apt-get install php5-dev  

$ whereis phpize
phpize: /usr/bin/phpize /usr/bin/X11/phpize /usr/share/man/man1/phpize.1.gz

(2)下载php5源码, 我准备保存在 ~/code/ 目录下

$ cd ~/code/
$ sudo apt-get source php5
下载的文件如下

$ ls
php5-5.4.9                         php5_5.4.9-4ubuntu2.dsc
php5_5.4.9-4ubuntu2.debian.tar.gz  php5_5.4.9.orig.tar.xz


二、生成扩展骨架文件
进入php的ext目录
$ cd ~/code/php5-5.4.9/ext/
执行一下命令
$ ./ext_skel --extname=xw
发现没有ext下权限没有权限创建目录,于是修改ext目录的权限
$ chmod 0777 ~/code/php5-5.4.9/ext

再次执行

$ ./ext_skel --extname=xw
ext下正常生成了xw目录,但目录中没有c文件,这和网络上教程所说有些不一样啊,仔细看了看命令的输出,发现有些报错说一些文件没有找到
cannot open /skeleton.c: No such file
vim 打开ext_skel文件

搜索“skeleton.c”,得

sed -f sedscript < $skel_dir/skeleton.c > $extname.c
继续搜索“skel_dir”
if test -z "$skel_dir"; then
  skel_dir="/usr/lib/php5/"
fi
原来是这个路径,查看我的电脑确实没有这个目录,那搜索一下是否是在其他地方呢
$ locate /skeleton
/usr/share/php5/skeleton
这个路径应该是可以配置的,试试help看看
$ ./ext_skel --help
./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]]
           [--skel=dir] [--full-xml] [--no-help]

  --extname=module   module is the name of your extension
  --proto=file       file contains prototypes of functions to create
  --stubs=file       generate only function stubs in file
  --xml              generate xml documentation to be added to phpdoc-cvs
  --skel=dir         path to the skeleton directory
  --full-xml         generate xml documentation for a self-contained extension
                     (not yet implemented)
  --no-help          don't try to be nice and create comments in the code
                     and helper functions to test if the module compiled

明白了,修改原来的命令,增加--skel参数再次执行
$ ./ext_skel --extname=xw --skel=/usr/share/php5/skeleton
ok了,生成的文件正常了


三、编译扩展

进入xw目录 ,vim config.m4 把

dnl PHP_ARG_ENABLE(xw, whether to enable xw support,
dnl Make sure that the comment is aligned:
dnl [  --enable-xw           Enable xw support])
这3行前面的“dnl”注释去掉,变成
PHP_ARG_ENABLE(xw, whether to enable xw support,
Make sure that the comment is aligned:
[  --enable-xw           Enable xw support])

依次执行

$ phpize
$ ./configure 
$ make
$ sudo make install
若执行成功了,会显示
Installing shared extensions:     /usr/lib/php5/20100525/
这时你在 /usr/lib/php5/20100525/ 目录找到 xw.so


四、修改php配置,运行检测

修改php.ini 配置文件,把xw.so扩展加入进去(这个就多不多说了)。若你不知道ini文件在哪,可以执行以下命令查看

$ php -ini
重启后,执行
$ php -r 'echo confirm_xw_compiled("xw"),"\n";'
若你能看到如下输出则你的扩展编译安装配置成功了
Congratulations! You have successfully modified ext/xw/config.m4. Module xw is now compiled into PHP.
其实,这句话就是xw.c里面函数返回给php的,你可以找到,并尝试修改它,然后重复 make和make install 步骤

函数confirm_xw_compiled()是扩展骨架自带的一个自定义函数,是一个例子,也可用于检查是否安成功安装了扩展。其中“xw”是扩展名







  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值