Come iniziare a scrivere un'estensione PHP5 in C ++
-
20-08-2019 - |
Domanda
Sto scrivendo un'estensione PHP5 e mentre potrei scriverla in C, sarebbe più facile usare C ++ e trarre vantaggio da STL e Boost.
Il problema è che i tutorial ho visto trattare solo con C, e sto cercando un esempio di base che utilizza C ++
Ecco cosa ho provato finora:
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
Nota il mio tentativo di dichiarare i bit con cui PHP si interfaccia come 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);
}
.... e qui ci sono i miei errori di costruzione:
Se phpize, configuro e realizzo questo, ottengo quanto segue (riformattato per chiarezza)
$ 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
Sospetto di aver bisogno di più lavoro per config.m4 per creare un makefile funzionante ma sono piuttosto nuovo nella toolchain di GCC.
Se aiuta, sto prendendo di mira solo php 5.2.6+ e solo su Linux (in particolare, Ubuntu 8.04). Il mio ambiente di compilazione utilizza Ubuntu 8.10, usando gcc 4.3.2
Puntatori ricevuti con gratitudine!
Soluzione
Dopo la pubblicazione mi sono imbattuto in CodeGen_PECL che crea un'estensione scheletro da una descrizione basata su XML dell'estensione. Questo include un tag che lo rende output C ++
Oltre ad assicurarsi che il file di intestazione utilizzato extern " C " ;, il file cpp generato ha anche assicurato che ZEND_GET_MODULE (ciao) fosse all'interno di un " C " esterno ; bloccare anche.
Come previsto, la differenza più grande era nel file m4, che sembrava così:
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
Quindi, se stai affrontando lo stesso problema, usa CodeGen_PECL , oppure adatta l'esempio m4 sopra (oltre a assicurarti di aver usato extern " C " nell'intestazione e attorno alla macro ZEND_GET_MODULE)
Altri suggerimenti
C'è anche questa introduzione rapida per racchiudere una classe (ed esportare funzioni in stile Class :: Method) qui: http://devzone.zend.com/article/4486
Per me la parte più utile sono le righe da aggiungere per il compilatore C ++ / regole di collegamento a config.m4 per phpize.