4

PHP扩展开发-自动生成扩展骨架

 2 years ago
source link: https://petrie.github.io/post/2018-03-29-first-php-extension/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

PHP扩展开发-自动生成扩展骨架

2018-03-29

PHP扩展编译有两种方式

  • 作为一个可装载模块或者DSO(动态共享对象)
  • 静态编译到PHP

静态编译的方式直接和PHP编译到一起,步骤比较简单,但每次变更代码都要编译这个PHP代码,非常耗时,所以这里采用第二种方式

本文脚本命令仅在以下环境测试成功

操作系统:CentOS 6.9
PHP版本:PHP-7.1

安装开发工具包

yum groupinstall -y 'Development Tools'

下载PHP源码

git clone http://git.php.net/repository/php-src.git
git checkout PHP-7.1
sudo yum install  libxml2-devel

编译安装php

安装到~/php7.1目录下,以免覆盖系统已安装的PHP

./buildconf
./configure --prefix=$HOME/php7.1 --enable-debug --enable-maintainer-zts
make && make install

生成扩展框架

cd $HOME/php-src/ext
$HOME/php-src/ext/ext_skel --extname=testex

编辑config.m4

去掉config.m4文件中第16、18行前的dnl注释

 16 PHP_ARG_ENABLE(testex, whether to enable testex support,
 17 dnl Make sure that the comment is aligned:
 18 [  --enable-testex           Enable testex support])
cd $HOME/php-src/ext/testex
$HOME/php7.1/bin/phpize
./configure --with-php-config=$HOME/php7.1/bin/php-config
make && make install

ext_skel脚本生成的扩展已包含confirm_testex_compiled函数,所以我们用它坐测试

$HOME/php7.1/bin/php -dextension=$HOME/php7.1/lib/php/extensions/debug-zts-20160303/testex.so -r "echo confirm_testex_compiled('1');"

本文只是用php源码自带的ext_skel生成扩展框架,测试编译和安装。扩展不包含任何功能。如果想加入更多功能,可以参考http://www.phpinternalsbook.com/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK