用 C 扩展 PHP?

发布于 2024-07-26 10:52:21 字数 104 浏览 7 评论 0原文

我有一个用 PHP 编写的性能密集型例程,我想将其移植到 C++ 以提高性能。 有没有办法使用 C++ 编写插件或扩展或其他东西并从 PHP 与之交互? 无需手动编辑实际的 PHP 源代码?

I have a performance intensive routine that is written in PHP that I'd like to port to C++ for a performance increase. Is there any way to write a plugin or extension or something using C++ and interface with it from PHP? WITHOUT manually editing the actual PHP source?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羁客 2024-08-02 10:52:21

正如 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)

config.m4 文件中的任何 C++ 库编译检查也需要链接 C++ lib:

PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,,
[
  AC_MSG_ERROR([lib $LIBNAME not found.])
],[
  -lstdc++ -ldl
])

编辑 - 以下是如何指定 g++:

最后,为了在构建扩展时选择 C++ 而不是 C 编译器/链接器,PHP_NEW_EXTENSION()< 的 第 6 参数< /code> 应为“是”。 即:

PHP_NEW_EXTENSION(your_extension,
                  your_extension.cpp, 
                  $ext_shared, 
                  ,
                  "-Wall -Werror -Wno-error=write-strings -Wno-sign-compare",
                  "yes")

来自 PHP 构建系统手册,参数为:

  1. 扩展名
  2. 属于扩展名的所有源文件列表。
  3. (可选)$ext_shared,当为(可选)“SAPI 类”调用 PHP_ARG_WITH() 时由配置确定的值
  4. ,仅对专门需要 CGI 或 CLI SAPI 的扩展有用。 在所有其他情况下,它应该留空。
  5. (可选)构建扩展时要添加到 CFLAGS 的标志列表。
  6. (可选)一个布尔值,如果“是”,将强制使用 $CXX 而不是 $CC 构建整个扩展。

<罢工>
我无法弄清楚如何让配置脚本将 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

< Strike>大概有一个 automake 命令可以使这种黑客攻击变得不必要。

As Remus says, you can extend PHP with C/C++ using the Zend API. The linked tutorial by Sara Golemon is a good start, and the book Extending and Embedding PHP by the same author covers the subject in much more detail.

However, it's worth noting that both of these (and pretty much everything else I found online) focus on C and don't really cover some tweaks you need to get C++ extensions working.

In the config.m4 file you need to explicitly link to the C++ standard library:

PHP_REQUIRE_CXX()
PHP_ADD_LIBRARY(stdc++, 1, PHP5CPP_SHARED_LIBADD)

Any C++ library compile checks in the config.m4 file will also require linking the C++ lib:

PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,,
[
  AC_MSG_ERROR([lib $LIBNAME not found.])
],[
  -lstdc++ -ldl
])

EDIT - and here's how to specify g++:

Last, and not least, in order to choose the C++ rather than C compiler/linker when building the extension, the 6th parameter to PHP_NEW_EXTENSION() should be "yes". ie:

PHP_NEW_EXTENSION(your_extension,
                  your_extension.cpp, 
                  $ext_shared, 
                  ,
                  "-Wall -Werror -Wno-error=write-strings -Wno-sign-compare",
                  "yes")

From the PHP build system manual, the parameters are:

  1. The name of the extension
  2. List of all source files which are part of the extension.
  3. (optional) $ext_shared, a value which was determined by configure when PHP_ARG_WITH() was called for
  4. (optional) "SAPI class", only useful for extensions which require the CGI or CLI SAPIs specifically. It should be left empty in all other cases.
  5. (optional) A list of flags to be added to CFLAGS while building the extension.
  6. (optional) A boolean value which, if "yes", will force the entire extension to be built using $CXX instead of $CC.


I couldn't work out how to get the configure script to set g++ as the compiler/linker instead of gcc, so ended up hacking the Makefile with a sed command to do a search replace in my bash build script:

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

Presumably there's an automake command that would make this hack unnecessary.

骑趴 2024-08-02 10:52:21

我在 SWIG 的帮助下用 C++ 编写了一个 PHP 插件。 这是可行的,但可能需要一段时间才能习惯 SWIG 编译周期。 您可以从PHP 的 SWIG 文档开始。

更新
正如 @therefromhere 所提到的,我强烈建议您阅读扩展和嵌入 PHP 这本书。 网上几乎找不到任何文档(至少在 2008 年末、2009 年初我做 PHP 插件时没有)。 我一切都必须依靠书本。 尽管有时 Google 代码搜索有助于查找示例代码。

I've written a PHP plugin in C++ with the help of SWIG. It's doable, but it may take a while to get used to the SWIG-compilation cycle. You can start with the SWIG docs for PHP.

Update
As @therefromhere has mentioned, I greatly recommend that you get the book Extending and Embedding PHP. There is almost no documentation to be found online (at least there wasn't in late 2008, early 2009 when I did my PHP plugin). I had to rely on the book for everything. Although sometimes Google Code Search is helpful for finding sample code.

请恋爱 2024-08-02 10:52:21

PHP 本身是松散相关的库的集合。 请参阅 http://devzone.zend.com/article/1021 了解如何编写的教程你自己。

PHP itself a collection of loosely related libraries. See http://devzone.zend.com/article/1021 for a tutorial how to write your own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文