Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to start writing a PHP5 extension in C++
    text
    copied!<p>I'm writing a PHP5 extension, and while I could write it in C, it would be easier to use C++ and take advantage of the STL and Boost.</p> <p>Trouble is, the <a href="http://devzone.zend.com/node/view/id/1021" rel="noreferrer">tutorials</a> I've seen only deal with C, and I'm looking for a basic example which uses C++</p> <p>Here's what I've tried so far:</p> <h3>config.m4</h3> <pre><code>[ --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 </code></pre> <h3>php_hello.h</h3> <p>Note my attempt to declare the bits that PHP interfaces with as extern "C"</p> <pre><code>#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 &amp;hello_module_entry } #endif </code></pre> <h3>hello.cpp</h3> <pre><code>#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 &gt;= 20010901 STANDARD_MODULE_HEADER, #endif PHP_HELLO_WORLD_EXTNAME, hello_functions, NULL, NULL, NULL, NULL, NULL, #if ZEND_MODULE_API_NO &gt;= 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); } </code></pre> <h3>....and here are my build errors:</h3> <p>If I phpize, configure and make this, I get the following (reformatted for clarity)</p> <pre><code>$ 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 </code></pre> <p>I suspect I need more work doing to the config.m4 in order to create a working makefile but I'm pretty new to the GCC toolchain.</p> <p>If it helps, I'm only targetting php 5.2.6+, and only on Linux (specifically, Ubuntu 8.04). My build environment is using Ubuntu 8.10, using gcc 4.3.2</p> <p>Pointers gratefully received!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload