您的位置:首页 > 编程语言 > PHP开发

编译php扩展

2016-05-19 14:18 651 查看
1) ext_skel

JesusSlimsMBFuxkingP:php5.6.6 jesusslim$ cd /Applications/MAMP/bin/php/php5.6.6/include/php
JesusSlimsMBFuxkingP:php jesusslim$ cd ext
JesusSlimsMBFuxkingP:ext jesusslim$ ./ext_skel --extname=hello_module

在这个目录下生成一个目录叫hello_module

进入

然后我们要修改文件顺序是

configue.m4

hello_module.c

php_hello_module.h

2)编辑config.m4文件,我们使用enable-hello_module选项:

dnl PHP_ARG_ENABLE(hello_module, whether to enablehello_module support,

dnl Make sure that the comment is aligned:

dnl [ --enable-hello_module      Enable hello_module support])

修改成
PHP_ARG_ENABLE(hello_module, whether to enable hello_module support,

[ --enable-hello_module Enable hello_module support])

3)vi hello_module.c

主要是给模块添加函数:

/* Every user visible function must have an entry in my_module_functions[].*/

function_entry hello_module_functions[] = {

    PHP_FE(hello_world,    NULL) /* 添加着一行代码 */

    PHP_FE(confirm_my_module_compiled,   NULL) /* For testing, remove later. */

    {NULL, NULL, NULL}   /* Must be the last line in my_module_functions[] */

};

在文件的最后添加下列代码

定义函数:

PHP_FUNCTION(hello_world)

{

    zend_printf("hello sdomain!");

}

4)修改php_hello_module.h

在PHP_FUNCTION(confirm_my_module_compiled ); /* For testing, remove later. */ 这行的下面添加一行:

PHP_FUNCTION(hello_world); /* 函数的声明 */

我这里直接在文件最底下加上

PHP_FUNCTION(hello_world); /* 函数的声明 */

5) 执行phpize并编译

JesusSlimsMBFuxkingP:hello_module jesusslim$ /Applications/MAMP/bin/php/php5.6.6/bin/phpize

然后执行:

JesusSlimsMBFuxkingP:hello_module jesusslim$ ./configure --enable-hello_module --with-php-config=/Applications/MAMP/bin/php/php5.6.6/bin/php-config
JesusSlimsMBFuxkingP:hello_module jesusslim$ make

这个时候会在当前的目录下生成一个目录叫modules先存放着hello_module.so文件

cp modules/hello_module.so /usr/local/php/ext/

然后再把hello_module.so文件拷到php.ini里面的extension_dir所指定的位置

6)开启扩展

php.ini文件中打开这个扩展

extension=hello_module.so

重新起动apache

用phpinfo来查看

调用直接hello_world();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: