Virink's Blog logo

Virink's Blog

Let life be beautiful like summer flowers, and death like autume leaves.

 PHP扩展-扩展的生成和编译

0x00 说明

PHP扩展有两种编译方式

  1. 在编译PHP时直接将扩展编译进去
  2. 扩展被编译成.so文件,在php.ini里配置加载路径

源码环境

先下载源码,可以通过git clone等,进入源码跟目录,进入插件目录

cd php-5.6.31
cd ext

0x01 创建PHP扩展

执行ext_skel命令生成扩展框架

./ext_skel --extname=demo

执行成功后会有一个操作步骤提示

To use your new extension, you will have to execute the following steps:

  1. $ cd ..
  2. $ vi ext/demo/config.m4
  3. $ ./buildconf
  4. $ ./configure –[with|enable]-demo
  5. $ make
  6. $ ./sapi/cli/php -f ext/demo/demo
  7. $ vi ext/demo/demo
  8. $ make

这是源码编译的步骤

0x02 修改文件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...

0x03-1 编译PHP时直接将扩展编译进去

cd php-src/
./buildconf or ./buildconf --force
./configure –enable-demo
make

大概这样子就OK了,但是我没有这样编译过,一般用另一种方式的。同时也推荐用另一种方式

0x03-2 编译成.so文件 【推荐】

cd php-src/ext/demo
phpize
./configure –enable-demo
make

然后扩展就是成功编译,并且保存在php-src/ext/demo/modules/demo.so

0x04 加载扩展

php.ini中,添加extension=/path/demo.dll或者zend_extension=/path/demo.so

根据不同的扩展类型选择不同的加载方式。

本文标题 : PHP扩展-扩展的生成和编译
文章作者 : Virink
发布时间 :  
最后更新 :  
本文链接 : https://www.virzz.com/2017/08/15/create_and_make_php_ext.html
转载声明 : 转载请保留原文链接及作者。
转载说明 : 本卡片有模板生成,本人转载来的文章请忽略~~