系统:centos
php版本:php5.6.3
安装过程:源码包进行编译
1. 生成php扩展骨架
PHP编译好之后,进入PHP源码包ext目录下,有个可执行文件ext_skel,这个文件是zend开发团队为了
方便扩展开发者快速构建扩展骨架而编写的,我们使用 --help 看看我们需要的参数
[root@wjw ext]# ./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
接下来我们使用到的两个参数分别是
extname :设置扩展模块的名称
proto :设置函数的原型的文件地址(函数原型是指c语言的函数原型)我们的目的是实现两个整数相加,我们创建一个原型文件 addtest.skel,内容如下
int addtest(int a,int b)
然后我们执行命令:
[root@wjw ext]# ./ext_skel --extname=addtest --proto=./addtest.skel
Creating directory addtest
awk: /usr/local/src/php-5.6.3/ext/skeleton/create_stubs:56: 警告: 转义序列“\|”被当作单纯的“|”
Creating basic files: config.m4 config.w32 .gitignore addtest.c php_addtest.h CREDITS EXPERIMENTAL tests/001.phpt addtest.php [done].
To use your new extension, you will have to execute the following steps:
1. $ cd ..
2. $ vi ext/addtest/config.m4
3. $ ./buildconf
4. $ ./configure --[with|enable]-addtest
5. $ make
6. $ ./sapi/cli/php -f ext/addtest/addtest.php
7. $ vi ext/addtest/addtest.c
8. $ make
Repeat steps 3-6 until you are satisfied with ext/addtest/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.
到这里,我们的扩展骨架就自动生成了。接下来,我们不遵循上面提示的步骤,当然你按照上面的来也可以,只是要重新构建整个php项目 并开启addtest扩展,我觉得没有必要,所以我们直接走了phpize,下面将会提及
2. 更改config.m4
我们将下面的三行前面的dnl去掉dnl PHP_ARG_WITH(addtest, for addtest support,
dnl Make sure that the comment is aligned:
dnl [ --with-addtest Include addtest support])
改成:
PHP_ARG_WITH(addtest, for addtest support,
Make sure that the comment is aligned:
[ --with-addtest Include addtest support])
然后保存
3. 修改addtest.c
我们找到函数 addtest
PHP_FUNCTION(addtest)
{
int argc = ZEND_NUM_ARGS();
long a;
long b;
if (zend_parse_parameters(argc TSRMLS_CC, "ll", &a, &b) == FAILURE)
return;
php_error(E_WARNING, "addtest: not yet implemented");
}
更改为:
PHP_FUNCTION(addtest)
{
int argc = ZEND_NUM_ARGS();
long a;
long b;
long sum;
if (zend_parse_parameters(argc TSRMLS_CC, "ll", &a, &b) == FAILURE)
return;
sum = a + b;
RETURN_LONG(sum);
}
4.在当前目录下执行phpize
会自动生成configure文件
[root@wjw addtest]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20131106
Zend Module Api No: 20131226
Zend Extension Api No: 220131226
5. configure生成make文件
[root@wjw addtest]# ./configure --with-php-config=/usr/local/php/bin/php-config
6. make
[root@wjw addtest]# make
7. make test
[root@wjw addtest]# make test
8. 将扩展加入指定扩展目录
执行完上面的命令,会在当前目录下生成一个modules文件夹会生成 addtest.so 扩展文件
我们看下,php的配置文件中扩展目录的是哪里
[root@wjw addtest]# /usr/local/php/bin/php -i | grep 'extension_dir'
extension_dir => /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226 => /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226
然后我们将我们的addtest.so 拷贝到扩展目录下
[root@wjw addtest]# cp ./modules/addtest.so /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
修改php.ini 文件,加入扩展
extension=“addtest.so”
然后使用php -r测试一下扩展是否编译完成
[root@wjw addtest]# /usr/local/php/bin/php -r "echo addtest(1,2);"
3
看到了我们期望的结果3,a+b完成。
有兴趣深入研究的可参看:http://www.cunmou.com/phpbook/preface.md