寻觅生命中的那一片浅草......

每日存档 四月 8th, 2010

关于MCV中用C编写module然后加载进PHP中

工具 swig

教程
1, 创建example.c
$cat example.c
int example(char *s) {
printf(“%s”, s);
}
2, 创建.o
cc -c example.c
得到 example.o
3, 创建example.i
$cat example.i
%module example
extern int example(char *s);
4, 创建wrap文件
$swig -php4 example.i
得到 example_wrap.c example.php php_example.h
5, 创建wrap.o
$cc -c example_wrap.c -I/usr/local/include/glib-2.0 -I/usr/local/include/php -I/usr/local/include/php/Zend -I/usr/local/include/php/TSRM -I/usr/local/include/php/main -I/usr/local/include/php/regex
得到 example_wrap.o
6, 生成模块文件
$cc -shared -o php_example.so example.o example_wrap.o

得到的php_example.so及我们想要的lib包,可以将它放入到php.ini所指定的extension_dir中去
然后可以加上
extension=php_example.so

7, 测试文件
<?php
$s = “我的选择取决于你”;
example($s);
?>

转载自:http://blog.csdn.net/abaowu/archive/2005/07/20/429862.aspx

我的经历:

cc -shared -o php_example.so example.o example_wrap.o

报错
/usr/bin/ld: example_wrap.o: relocation R_X86_64_32S against `a local symbol’ can not be used when making a shared object; recompile with -fPIC

example_wrap.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
加-fPIC参数后,可以顺利编译通过
cc -c -fPIC example.c
cc -c example_wrap.c -fPIC -I/usr/include/glib-2.0 -I/usr/local/php/include/php -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/main -I/usr/local/php/include/php/regex

2010年四月
« 3月   5月 »
 1234
567891011
12131415161718
19202122232425
2627282930