题
我正在写一个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
请注意我试图声明的位与为extern“C” PHP接口
#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,配置,使这个,我得到以下(重新格式化为清楚起见)
$ 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
指针感激地接受!
解决方案
张贴后予跨越 CodeGen_PECL 从基于XML的描述创建一个骨架延伸出来的延伸。这包括一个标签使它输出C ++
除了确保使用外部的“C”的头文件,将所生成的文件的cpp也保证了ZEND_GET_MODULE(你好)是一个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样品(以及确保您已经使用的extern“C”在头和周围的ZEND_GET_MODULE宏)
其他提示
还有这样匆匆介绍到包装类(和出口在类::方法风格功能)在这里: http://devzone.zend.com/article/4486
有关我最有用的部分是/添加用于C ++编译器的连接规则的config.m4为phpize的行。
不隶属于 StackOverflow