链接用C语言编写的PHP扩展 [英] Linking a PHP Extension Written in C
问题描述
修改我的问题
在C语言中构建外部PHP模块时,如何链接共享对象?
When building an external PHP module in C, how do I link shared objects?
推荐答案
如果您的C扩展代码使用共享库,则需要在config.m4
文件中声明它.
If your C extension code uses a shared library, you need to declare that in the config.m4
file.
我强烈建议使用 ext_skel
脚本包含在PHP源代码中以生成框架config.m4:
I strongly recommend using the ext_skel
script that's included in the PHP source to generate a skeleton config.m4:
./ext_skel --extname=myextension
由于要链接到库,因此按照惯例,应使用--with-myextension
选项(而不是--enable-myextension
).取消注释config.m4中的相关行,并填写您的lib的详细信息.
Since you're linking to a library, by convention you should use the --with-myextension
options (as opposed to --enable-myextension
). Uncomment the relevant lines in the config.m4 and fill in the details of your lib.
类似以下内容:
# --with-myextension -> check for lib and symbol presence
LIBNAME=the_lib_your_extension_needs # you may want to change this
LIBSYMBOL=some_symbol_in_the_lib_you_extension_needs # you most likely want to change this
PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
[
PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $MYEXTENSION_DIR/lib, MYEXTENSION_SHARED_LIBADD)
AC_DEFINE(HAVE_MYEXTENSIONLIB,1,[ ])
],[
AC_MSG_ERROR([wrong $LIBNAME lib version or lib not found])
],[
-L$MYEXTENSION_DIR/lib -ldl
])
然后构建它,运行:
phpize
./configure --with-myextension
make
最后,您需要将模块(或ln -s
)复制到系统希望找到它的位置.
Finally you need to copy your module (or ln -s
) to wherever your system expects to find it.
如果一切正常,那么php -m
应将您的模块包括在列表中.
If that all worked then php -m
should include your module in the list.
不幸的是,我从未在网上找到关于PHP的config.m4命令的很好的参考-有关该书的信息是 Sara Golemon的扩展和嵌入PHP 以及乔治·施洛斯纳格(George Schlossnagle)的高级PHP编程.
Unfortunately I've never found a good online reference to PHP's config.m4 commands - the books for this are Sara Golemon's Extending and Embedding PHP and also parts of George Schlossnagle's Advanced PHP Programming.
Sara Goleman提供了一个合理的初学者指南来创建PHP扩展在这里,但是对于肉类,您真的需要她的书.
There's a reasonable beginners guide to creating PHP extensions by Sara Goleman here, but for the meat you really need her book.
这篇关于链接用C语言编写的PHP扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!