How to start writing a PHP5 extension in C++

删除回忆录丶 提交于 2019-11-28 16:53:50

After posting I came across CodeGen_PECL which creates a skeleton extension from an XML based description of the extension. This includes a tag make it output C++

As well as making sure the header file used extern "C", the generated cpp file also ensured the ZEND_GET_MODULE(hello) was inside an extern "C" block also.

As expected, the biggest difference was in the m4 file, which looked like this:

dnl
dnl $ Id: $
dnl

PHP_ARG_ENABLE(hello, whether to enable hello functions,
[  --enable-hello         Enable hello support])

if test "$PHP_HELLO" != "no"; then
  PHP_REQUIRE_CXX
  AC_LANG_CPLUSPLUS
  PHP_ADD_LIBRARY(stdc++,,HELLO_SHARED_LIBADD)
  export OLD_CPPFLAGS="$CPPFLAGS"
  export CPPFLAGS="$CPPFLAGS $INCLUDES -DHAVE_HELLO"

  AC_MSG_CHECKING(PHP version)
  AC_TRY_COMPILE([#include <php_version.h>], [
#if PHP_VERSION_ID < 40000
#error  this extension requires at least PHP version 4.0.0
#endif
],
[AC_MSG_RESULT(ok)],
[AC_MSG_ERROR([need at least PHP 4.0.0])])

  export CPPFLAGS="$OLD_CPPFLAGS"


  PHP_SUBST(HELLO_SHARED_LIBADD)
  AC_DEFINE(HAVE_HELLO, 1, [ ])

  PHP_NEW_EXTENSION(hello, hello.cpp , $ext_shared)

fi

So, if you're struggling with the same problem, use CodeGen_PECL, or adapt the m4 sample above (as well as making sure you've used extern "C" in your header and around the ZEND_GET_MODULE macro)

There's also this quickie intro to wrapping a class (and exporting functions in Class::Method style) here: http://devzone.zend.com/article/4486

For me the most useful part is the lines to add for C++ compiler/linking rules to config.m4 for phpize.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!