如何开始用C++编写PHP5扩展

20

我正在编写一个PHP5扩展,虽然我可以用C语言编写它,但使用C++并利用STL和Boost会更容易。

问题是,我看到的教程只涉及C语言,我正在寻找一个基本示例,其中使用了C++。

这是我迄今为止尝试过的:

config.m4

[ --enable-hello   Enable Hello World support])

if test "$PHP_HELLO" = "yes"; then
  AC_DEFINE(HAVE_HELLO, 1, [Whether you have Hello World])
  PHP_NEW_EXTENSION(hello, hello.cpp, $ext_shared)
fi

php_hello.h

请注意我尝试将 PHP 接口与 extern "C" 声明的位联系起来。

#ifndef PHP_HELLO_H
#define PHP_HELLO_H 1


extern "C" {

#define PHP_HELLO_WORLD_VERSION "1.0"
#define PHP_HELLO_WORLD_EXTNAME "hello"

PHP_FUNCTION(hello_world);

extern zend_module_entry hello_module_entry;
#define phpext_hello_ptr &hello_module_entry

}
#endif

hello.cpp

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_hello.h"

static function_entry hello_functions[] = {
    PHP_FE(hello_world, NULL)

    {NULL, NULL, NULL}
};

zend_module_entry hello_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
    STANDARD_MODULE_HEADER,
#endif
    PHP_HELLO_WORLD_EXTNAME,
    hello_functions,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
#if ZEND_MODULE_API_NO >= 20010901
    PHP_HELLO_WORLD_VERSION,
#endif
    STANDARD_MODULE_PROPERTIES
};

#ifdef COMPILE_DL_HELLO
ZEND_GET_MODULE(hello)
#endif

PHP_FUNCTION(hello_world)
{
    RETURN_STRING("Hello World", 1);
}

......这是我的构建错误:

如果我使用phpize、configure和make,我会得到以下结果(为了清晰起见重新格式化)

$ make
/bin/bash /home/paul/php5/php-5.2.8/ext/hello2/libtool 
   --mode=compile  
   -I. 
   -I/home/paul/php5/php-5.2.8/ext/hello2 -DPHP_ATOM_INC 
   -I/home/paul/php5/php-5.2.8/ext/hello2/include 
   -I/home/paul/php5/php-5.2.8/ext/hello2/main 
   -I/home/paul/php5/php-5.2.8/ext/hello2 
   -I/usr/local/include/php 
   -I/usr/local/include/php/main 
   -I/usr/local/include/php/TSRM 
   -I/usr/local/include/php/Zend 
   -I/usr/local/include/php/ext 
   -I/usr/local/include/php/ext/date/lib  
   -DHAVE_CONFIG_H     
   -c /home/paul/php5/php-5.2.8/ext/hello2/hello.cpp 
   -o hello.lo 
libtool: compile: unrecognized option `-I.'
libtool: compile: Try `libtool --help' for more information.
make: *** [hello.lo] Error 1

我怀疑需要在config.m4中进行更多的工作,以创建一个可工作的makefile,但我对GCC工具链还不太熟悉。

如果有帮助的话,我只针对php 5.2.6+,并且仅在Linux上(具体来说是Ubuntu 8.04)进行目标设置。我的构建环境使用Ubuntu 8.10,使用gcc 4.3.2

非常感谢您的指点!


刚刚发现了http://php-baustelle.de/CodeGen_PECL/manual.html,它似乎可以创建一个与C++代码链接的骨架扩展。我会试一下... - Paul Dixon
2个回答

5
在发布后,我发现了CodeGen_PECL,它可以从基于XML的扩展描述创建骨架扩展。其中包括一个标记,使其输出C++。

除了确保头文件使用extern "C"之外,生成的cpp文件还确保ZEND_GET_MODULE(hello)也在extern "C"块中。

如预期的那样,最大的区别在于m4文件,它看起来像这样:

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

如果你也遇到同样的问题,可以使用CodeGen_PECL,或者根据上面提供的m4示例进行修改(同时确保在头文件和ZEND_GET_MODULE宏周围使用了extern "C")。


1

这里还有一个快速介绍如何包装类(以及以Class::Method风格导出函数)的入门教程:http://devzone.zend.com/article/4486

对我来说,最有用的部分是在config.m4中添加的C++编译器/链接规则的行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接