PHP扩展-扩展的生成和编译
2016-11-17 10:16
162 查看
首先说明一下,PHP扩展有两种编译方式:
方式一:在编译PHP时直接将扩展编译进去
方式二:扩展被编译成.so文件,在php.ini里配置加载路径;
以下开始说明创建PHP扩展并编译的步骤:
下载PHP源码,并解压,在源码的根目录下开始操作,
1. 使用ext_skel生成扩展框架,如下:
➜ php-5.6.24 cd ~/Downloads/tmp/php-5.6.24
➜ php-5.6.24 cd ext
➜ ext ./ext_skel --extname=myfirstext
ext_skel在执行后,会提示开发者后续的操作步骤,这个操作步骤是扩展的两种编译方式里的方式一的步骤, 如下:
To use your new extension, you will have to execute the following steps:
1. $ cd ..
2. $ vi ext/plogger/config.m4
3. $ ./buildconf
4. $ ./configure --[with|enable]-plogger
5. $ make
6. $ ./sapi/cli/php -f ext/plogger/plogger.php
7. $ vi ext/plogger/plogger.c
8. $ make
2. 修改文件ext/myfirstext/config.m4
重点看line10-18的代码,用于设置./configure时启用此扩展的命令选项,将其中line16和line18的dnl删掉,把dnl理解为注释符。
14 dnl Otherwise use enable:
15
16 dnl PHP_ARG_ENABLE(myfirstext, whether to enable myfirstext support,
17 dnl Make sure that the comment is aligned:
18 dnl [ --enable-myfirstext Enable myfirstext support])
19
20 if test "$PHP_MYFIRSTEXT" != "no"; then
21 dnl Write more examples of tests here...
以上两步骤是公共的,以下将分别介绍编译PHP扩展的两种方式,
方式一:编译PHP时直接将扩展编译进去
3. 在源码根目录下执行./buildconf,如下
4. 在源码根目录下执行./configure –enable-myfirstext
为了减少编译时间,可以在configure阶段指明不编译某些模块,比如:
./configure --without-iconv --enable-debug --enable-myfirstext --disable-cgi --enable-cli --without-pear --disable-xml --without-mysql
5. 在源码根目录下执行make
注意编译成功后,别执行make install了,因为至此,扩展myfirstext已经编译成功,并且已经生成了相应的php二进制文件了,它在./sapi/cli/php
方式二:扩展被编译成.so文件,在php.ini里配置加载路径
3. 在扩展目录ext/myfirstext/下执行phpize命令
4. 在扩展目录ext/myfirstext/下执行./configure –enable-myfirstext命令
5. 在扩展目录ext/myfirstext/下执行make
执行make后会在ext/myfirstext/modules下生成对应的.so文件,在php.ini中配置好加载此文件即可。
校验扩展是否加载成功
执行./sapi/cli/php -f ext/myfirstext/myfirstext.php
或者通过php -m列出所有扩展,查看是否有myfirstext, 执行命令:./sapi/cli/php -m | grep myfirstext
通过以上校验,说明扩展编译成功了。但是到目前为止,还没有编辑过c相关的代码,一切都是ext_skel默认生成的,查看这个扩展myfirstext包含哪些函数呢?如下:
➜ php-5.6.24 ./sapi/cli/php -r 'print_r(get_extension_funcs("myfirstext"));'
OK, 目前为止熟悉了PHP扩展框架的生成,配置,和编译。接下来就要往扩展myfirstext里添加一个自己的函数。
方式一:在编译PHP时直接将扩展编译进去
方式二:扩展被编译成.so文件,在php.ini里配置加载路径;
以下开始说明创建PHP扩展并编译的步骤:
下载PHP源码,并解压,在源码的根目录下开始操作,
1. 使用ext_skel生成扩展框架,如下:
➜ php-5.6.24 cd ~/Downloads/tmp/php-5.6.24
➜ php-5.6.24 cd ext
➜ ext ./ext_skel --extname=myfirstext
ext_skel在执行后,会提示开发者后续的操作步骤,这个操作步骤是扩展的两种编译方式里的方式一的步骤, 如下:
To use your new extension, you will have to execute the following steps:
1. $ cd ..
2. $ vi ext/plogger/config.m4
3. $ ./buildconf
4. $ ./configure --[with|enable]-plogger
5. $ make
6. $ ./sapi/cli/php -f ext/plogger/plogger.php
7. $ vi ext/plogger/plogger.c
8. $ make
2. 修改文件ext/myfirstext/config.m4
重点看line10-18的代码,用于设置./configure时启用此扩展的命令选项,将其中line16和line18的dnl删掉,把dnl理解为注释符。
14 dnl Otherwise use enable:
15
16 dnl PHP_ARG_ENABLE(myfirstext, whether to enable myfirstext support,
17 dnl Make sure that the comment is aligned:
18 dnl [ --enable-myfirstext Enable myfirstext support])
19
20 if test "$PHP_MYFIRSTEXT" != "no"; then
21 dnl Write more examples of tests here...
以上两步骤是公共的,以下将分别介绍编译PHP扩展的两种方式,
方式一:编译PHP时直接将扩展编译进去
3. 在源码根目录下执行./buildconf,如下
4. 在源码根目录下执行./configure –enable-myfirstext
为了减少编译时间,可以在configure阶段指明不编译某些模块,比如:
./configure --without-iconv --enable-debug --enable-myfirstext --disable-cgi --enable-cli --without-pear --disable-xml --without-mysql
5. 在源码根目录下执行make
注意编译成功后,别执行make install了,因为至此,扩展myfirstext已经编译成功,并且已经生成了相应的php二进制文件了,它在./sapi/cli/php
方式二:扩展被编译成.so文件,在php.ini里配置加载路径
3. 在扩展目录ext/myfirstext/下执行phpize命令
4. 在扩展目录ext/myfirstext/下执行./configure –enable-myfirstext命令
5. 在扩展目录ext/myfirstext/下执行make
执行make后会在ext/myfirstext/modules下生成对应的.so文件,在php.ini中配置好加载此文件即可。
校验扩展是否加载成功
执行./sapi/cli/php -f ext/myfirstext/myfirstext.php
或者通过php -m列出所有扩展,查看是否有myfirstext, 执行命令:./sapi/cli/php -m | grep myfirstext
通过以上校验,说明扩展编译成功了。但是到目前为止,还没有编辑过c相关的代码,一切都是ext_skel默认生成的,查看这个扩展myfirstext包含哪些函数呢?如下:
➜ php-5.6.24 ./sapi/cli/php -r 'print_r(get_extension_funcs("myfirstext"));'
OK, 目前为止熟悉了PHP扩展框架的生成,配置,和编译。接下来就要往扩展myfirstext里添加一个自己的函数。
![](http://blog.beatzeus.com/wp-content/uploads/2016/10/qrcode_for_gh_61c6224cfae9_258.jpg)
相关文章推荐
- Linux PHP编译生成扩展与修改配置
- 用apache的动态so模式编写和编译php扩展
- linux下php扩展pdo编译安装
- PHP: 用 phpize 编译共享 PECL 扩展库 - Manual
- 所谓开源,原来是啥软件都要自己来编译——CentOS下php扩展库的安装
- linux 下php扩展模块的单独编译
- php5编译无法生成 libphp5.so
- centOS 5.6 编译安装Memcached 与Memcache的PHP扩展
- windows下开发并编译PHP扩展的方法
- windows下开发并编译PHP扩展的方法
- linux 下编译安装php mongodb扩展
- PHP生成网页快照, 不用COM不用扩展.
- xp下用Cygwin模拟unix编译php和扩展
- win下php扩展vc6编译
- 中国银联php扩展编译方法
- 用apache的动态so模式编写和编译php扩展
- 不重新编译PHP的情况下独立添加扩展
- PHP扩展编写与编译
- 关于windows下面编译PHP扩展
- linux(fedora)下freetds编译安装及编译php的mssql扩展