rye*_*guy 13 php c++ plugins php-extension
我有一个用PHP编写的性能密集型例程,我想将其移植到C++以提高性能.有没有办法用PHP编写插件或扩展或其他什么东西?没有手动编辑实际的PHP源代码?
Joh*_*ter 20
正如Remus所说,您可以使用Zend API使用C/C++扩展PHP.Sara Golemon的链接教程是一个良好的开端,同一作者的" 扩展和嵌入PHP "一书更详细地介绍了该主题.
但是,值得注意的是,这些(以及我在网上找到的其他所有内容)都集中在C上,并没有真正涵盖了使C++扩展工作所需的一些调整.
在config.m4文件中,您需要显式链接到C++标准库:
PHP_REQUIRE_CXX()
PHP_ADD_LIBRARY(stdc++, 1, PHP5CPP_SHARED_LIBADD)
Run Code Online (Sandbox Code Playgroud)
任何C++库编译检查config.m4文件都需要链接C++库:
PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,,
[
AC_MSG_ERROR([lib $LIBNAME not found.])
],[
-lstdc++ -ldl
])
Run Code Online (Sandbox Code Playgroud)
最后,并非最不重要的是,为了在构建扩展时选择C++而不是C编译器/链接器,应该是第6个参数.即:PHP_NEW_EXTENSION()"yes"
PHP_NEW_EXTENSION(your_extension,
your_extension.cpp,
$ext_shared,
,
"-Wall -Werror -Wno-error=write-strings -Wno-sign-compare",
"yes")
Run Code Online (Sandbox Code Playgroud)
从PHP构建系统手册中,参数是:
$ext_shared,一个在调用PHP_ARG_WITH()时由configure确定的值
我无法弄清楚如何让configure脚本将g ++设置为编译器/链接器而不是gcc,所以最后用sed命令攻击Makefile以在我的bash构建脚本中执行搜索替换:
phpize
./configure --with-myextension
if [ "$?" == 0 ]; then
# Ugly hack to force use of g++ instead of gcc
# (otherwise we'll get linking errors at runtime)
sed -i 's/gcc/g++/g' Makefile
make clean
make
fi
Run Code Online (Sandbox Code Playgroud)
据推测,有一个automake命令会使这个hack变得不必要.