分享

php 扩展模块c库

 looline 2007-07-28

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(如果已经安装apacheweb服务器的话)。

这里采用phpbin测试,不需要任何web服务器。

拷贝exthelloword.sophpbin的相应目录下

(如果不知道是哪个目录,或者没有拷贝到正确的位置,在运行的时候出错信息里会指出应该在什么路径。)
# 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.cphp_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.soa.ha.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.soa.so都拷贝到phpbin/lib/php/extensions/下。

2.
调用静态库a.a

需要的文件,a.soa.ha.h主要做数据类型声明和入口函数声明),都放在 exthelloword目录下。

调用a.a

1)
2)都与调用a.so相同。

3)
测试exthelloword.so时,把exthelloword.so拷贝到phpbin/lib/php/extensions/下,a.a不需要。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多