Pau*_*xon 20 php c++ gcc php-extension
我正在编写PHP5扩展,虽然我可以用C语言编写,但使用C++并利用STL和Boost更容易.
麻烦的是,我看过的教程只涉及C,我正在寻找一个使用C++的基本例子
这是我到目前为止所尝试的:
[ --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
Run Code Online (Sandbox Code Playgroud)
注意我试图将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
Run Code Online (Sandbox Code Playgroud)
#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);
}
Run Code Online (Sandbox Code Playgroud)
如果我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
Run Code Online (Sandbox Code Playgroud)
我怀疑我需要为config.m4做更多的工作才能创建一个工作的makefile,但我对GCC工具链很新.
如果它有帮助,我只针对php 5.2.6+,并且只针对Linux(特别是Ubuntu 8.04).我的构建环境使用的是Ubuntu 8.10,使用的是gcc 4.3.2
指针感激不尽!
发布后,我遇到了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
Run Code Online (Sandbox Code Playgroud)
因此,如果您正在努力解决同样的问题,请使用CodeGen_PECL,或调整上面的m4示例(以及确保您在标题和ZEND_GET_MODULE宏周围使用了extern"C")
| 归档时间: |
|
| 查看次数: |
9888 次 |
| 最近记录: |