Frage

Ich schreibe eine PHP5-Erweiterung, und während ich es in C schreiben könnte, wäre es einfacher, C ++ und die Vorteile der STL nehmen und Boost.

Das Problem ist, das Tutorials ich nur mit C beschäftigt gesehen haben, und ich bin auf der Suche nach einem einfachen Beispiel, das verwendet C ++

Hier ist, was ich versucht habe, so weit:

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

Beachten Sie meinen Versuch, die Bits, die PHP-Schnittstellen mit als extern "C"

zu erklären,
#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);
}

.... und hier sind meine Buildfehler:

Wenn ich phpize, konfigurieren und diese machen, erhalte ich die folgenden (aus Gründen der Klarheit neu formatiert)

$ 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

Ich vermute, dass ich mehr Arbeit muß die config.m4 tun, um eine Arbeits Make-Datei zu erstellen, aber ich bin ziemlich neu in dem GCC-Toolchain.

Wenn es hilft, ich bin Targeting nur PHP 5.2.6+, und nur unter Linux (genauer gesagt, Ubuntu 8.04). Meine Build-Umgebung wird mit Ubuntu 8.10, mit gcc 4.3.2

Pointers dankbar empfangen!

War es hilfreich?

Lösung

Nach der Buchung stieß ich auf CodeGen_PECL , die aus einer XML-basierten Beschreibung ein Skelett Erweiterung schafft die Verlängerung. Dazu gehören ein Tag setzen Ausgang C ++

machen

Neben sicherstellen, dass die Header-Datei verwendet extern „C“, die erzeugte CPP-Datei gewährleistet auch die ZEND_GET_MODULE (hallo) war innen ein extern „C“ Block auch.

Wie erwartet, war der größte Unterschied in der m4-Datei, die wie folgt aussah:

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

Wenn Sie also mit dem gleichen Problem zu kämpfen haben, verwenden Sie CodeGen_PECL oder passen Sie die m4 Probe oben (wie auch dafür, dass Sie extern „C“ in der Kopfzeile und um das ZEND_GET_MODULE Makro verwendet haben)

Andere Tipps

Es gibt auch dieses Quickie Intro, eine Klasse zu wickeln (und Export-Funktionen in der Klasse :: Method Stil) hier: http://devzone.zend.com/article/4486

Für mich ist der nützlichste Teil ist die Linien für C ++ Compiler hinzuzufügen / Regeln Verknüpfung für phpize config.m4.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top