How to start writing a PHP5 extension in C++

前端 未结 2 355
-上瘾入骨i
-上瘾入骨i 2020-12-13 00:50

I\'m writing a PHP5 extension, and while I could write it in C, it would be easier to use C++ and take advantage of the STL and Boost.

Trouble is, the tutorials I\'v

相关标签:
2条回答
  • 2020-12-13 01:23

    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.

    0 讨论(0)
  • 2020-12-13 01:35

    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)

    0 讨论(0)
提交回复
热议问题