php 扩展模块c库

来源:百度文库 编辑:神马文学网 时间:2024/07/05 13:33:16
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文件,在里面写入
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不需要。