php c 扩展,php 扩展模块c库

php bin,到网上下载一个php源码包,解压,安装。php的解压目录记为phpsrc(如:/home/src/php-4.4.4),安装目录记为phpbin(如/usr/local/php)在shell下输入(以后遇到有shell的地方我就用#开头,不另陈述)# cd phpsrc/ext

# ./ext_skel --extname=exthelloword

Creating directory exthelloword

Creating basic files: config.m4 .cvsignore exthelloword.c php_exthelloword.h CREDITS EXPERIMENTAL tests/001.phpt exthelloword.php [done].

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

1. $ cd ..

2. $ vi ext/exthelloword/config.m4

3. $ ./buildconf

4. $ ./configure --[with|enable]-exthelloword

5. $ make

6. $ ./php -f ext/exthelloword/exthelloword.php

7. $ vi ext/exthelloword/exthelloword.c

8. $ make

Repeat steps 3-6 until you are satisfied with ext/exthelloword/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.系统自动生成exthelloword文件夹;接下来我们要修改几个文件:config.m4, exthelloword.c,php_exthelloword.h,如下:1)修改config.m4

# cd exthelloword

# vi config.m4找到这几行dnl PHP_ARG_ENABLE(exthelloword, whether to enable exthelloword support,dnl Make sure that the comment is aligned:

dnl [ --enable-exthelloword Enable exthelloword support])去掉这几行前面的dnl,改为PHP_ARG_ENABLE(exthelloword, whether to enable exthelloword support,Make sure that the comment is aligned:

[ --enable-exthelloword Enable exthelloword support])这样以后编译php时,./configure后面加--enable-exthelloword就可以加载你的php模块了!(接下来的2)你可以做也可以不做,直接跳到第4步也可以运行。)2)修改exthelloword.c,输出自己想要的东西# vi exthelloword.c找到这段PHP_FUNCTION(confirm_exthelloword_compiled)

{

char *arg = NULL;

int arg_len, len;

char string[256];

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {

return;

}

len = sprintf(string, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "exthelloword", arg);

RETURN_STRINGL(string, len, 1);

}改为:PHP_FUNCTION(confirm_exthelloword_compiled)

{

zend_printf("This is a exthelloword !");

}

3)编译链接# cd phpsrc/ext

# cc -fpic -DCOMPILE_DL_EXTHELLOWORD=1 -I/usr/local/include -I. -I../main -I.. -I../TSRM -I../Zend -c -o exthelloword/exthelloword.o exthelloword/exthelloword.c执行完之后会在目录下生成一个exthelloword.o文件,接下来连接:# cc -shared -L/usr/local/lib -rdynamic -o exthelloword/exthelloword.so exthelloword/exthelloword.o

4)测试:有两种途径可以测试你的扩展模块是否正确,一是在phpbin目录下运行test.php,二是在web browser上运行test.php(如果已经安装apache等web服务器的话)。这里采用phpbin测试,不需要任何web服务器。拷贝exthelloword.so到phpbin的相应目录下(如果不知道是哪个目录,或者没有拷贝到正确的位置,在运行的时候出错信息里会指出应该在什么路径。)# mkdir -p phpbin/lib/php/extensions/(其实这个目录就是看你php.ini里设置extension_dir了)# cp exthelloword/exthelloword.so phpbin/lib/php/extensions/在phpbin目录下新建一个test.php文件,在里面写入<?php

dl("exthelloword.so");

//调用函数exthelloword();

?>在phpbin目录下运行执行./php –q test.php,如果过程无误,将会显示:This is a exthelloword !测试成功,接下来我们可以往扩展模块中添加自己的函数,实现自己的功能啦。二.向扩展模块中添加函数向php扩展模块中添加函数,只需要修改exthelloword.c和php_exthelloword.h。比如要向扩展模块中添加一个函数sayhello(),我们需要做以下工作:1)修改php_exthelloword.h添加函数声明在文件中PHP_FUNCTION(confirm_my_module_compiled);一行前面添加下面的代码PHP_FUNCTION(say_hello);保存文件退出2)修改exthelloword.c在function_entry中添加函数的entry:function_entry my_module_functions[] = {

PHP_FE(say_hello, 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(say_hello)

{

zend_printf("hello world\n");

}保存文件退出3)重新编译链接,生成新的.so文件。# cd phpsrc/ext

# cc -fpic -DCOMPILE_DL_EXTHELLOWORD=1 -I/usr/local/include -I. -I../main -I.. -I../TSRM -I../Zend -c -o exthelloword/exthelloword.o exthelloword/exthelloword.c执行完之后会在目录下生成一个exthelloword.o文件,接下来连接:# cc -shared -L/usr/local/lib -rdynamic -o exthelloword/exthelloword.so exthelloword/exthelloword.o三.调用第三方C/C++库在PHP扩展模块中调用第三方的C/C++库,与普通的C/C++程序调用C/C++库一样,只要把库文件的位置放对就可以了。下面举例说明:1.调用动态库a.so需要的文件,a.so,a.h(a.h主要做数据类型声明和入口函数声明),都放在exthelloword目录下。调用a.so:1)在exthelloword.c的开头添加a.h:#include "php.h

#include "php_ini.h"

#include "a.h"添加了头文件后,可以在exthelloword.c的函数中调用a.so的函数接口。2)重新编译链接,链接时加入a.so:# cd phpsrc/ext

# cc -fpic -DCOMPILE_DL_EXTHELLOWORD=1 -I/usr/local/include -I. -I../main -I.. -I../TSRM -I../Zend -c -o exthelloword/exthelloword.o exthelloword/exthelloword.c执行完之后会在目录下生成一个exthelloword.o文件,接下来连接:# cc -shared -L/usr/local/lib -rdynamic -o exthelloword/exthelloword.so exthelloword/exthelloword.o exthelloword/a.so

[如果a.so内部实现是C++,链接时还应该加入参数–lstdc++,即:# cc –shared –lstdc++ -L/usr/local/lib -rdynamic -o exthelloword/exthelloword.so exthelloword/exthelloword.o exthelloword/a.so

]

3)测试exthelloword.so时,把exthelloword.so和a.so都拷贝到phpbin/lib/php/extensions/下。2.调用静态库a.a需要的文件,a.so,a.h(a.h主要做数据类型声明和入口函数声明),都放在exthelloword目录下。调用a.a:1)和2)都与调用a.so相同。3)测试exthelloword.so时,把exthelloword.so拷贝到phpbin/lib/php/extensions/下,a.a不需要。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值