转自个人博客:
https://www.tanchengjin.com/article/109
测试环境ubuntu:14.04
首先在php官网下载php包
wget
http://cn2.php.net/get/php-7.1.27.tar.gz/from/this/mirror
下载之后的包名位mirror 直接使用tar解压即可
tar -zxvf mirror
解压后进入ext文件夹
cd php-7.1.27/ext/
利用.ext_skel生成项目骨架,并在当前目录中生成helloWorld文件夹
./ext_skel --extname=helloWorld
提示生成扩展的步骤:
=======================================================
Creating directory helloWorld
Creating basic files: config.m4 config.w32 .gitignore helloWorld.c php_helloWorld.h CREDITS EXPERIMENTAL tests/001.phpt helloWorld.php [done].
To use your new extension, you will have to execute the following steps:
1. $ cd ..
2. $ vi ext/helloWorld/config.m4
3. $ ./buildconf
4. $ ./configure --[with|enable]-helloWorld
5. $ make
6. $ ./sapi/cli/php -f ext/helloWorld/helloWorld.php
7. $ vi ext/helloWorld/helloWorld.c
8. $ make
Repeat steps 3-6 until you are satisfied with ext/helloWorld/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.
=============================================================
进入项目文件夹
cd helloWorld
编辑config.m4文件
vim config.m4
去掉这两行的dnl(dnl为注释符,这段话是说如果此扩展依赖其他扩展,去掉
PHP_ARG_WITH
段的注释符;否则去掉
PHP_ARG_ENABLE
段的注释符。显然我们不依赖其他扩展或lib库,所以去掉
PHP_ARG_ENABLE
段的注释符:)

编辑helloWorld.c文件
vim helloWorld.c
注释掉自动生成的方法
重写PHP_FUNCTION方法
添加到编译列表里,同时注释掉confirm_helloWorld_compiled
让zend引擎知道此模块有什么函数
保存退出
执行phpize
phpize
生成.so文件
./configure --with-php-config=/usr/bin/php-config
编译
make && make install
编译成功后会在项目文件夹/ext/helloWorld/modules下生成helloWorld.so文件
编写helloWorld.php文件,写入如下代码
<?php
echo helloWorld();
执行
php helloWorld.php
就可以看到执行结果
转自个人博客:
https://www.tanchengjin.com/article/109