Re: PHP / C++ Interaction

From: Date: Mon, 20 Feb 2006 01:24:26 +0000
Subject: Re: PHP / C++ Interaction
References: 1 2  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
Just search the archives, or the zend weeklies index page (http://www.zend.com/zend/week/) if you're short of time. I remember linking to someone's material regarding C++ extensions for PHP a few months back, and it'll be in the 'item descriptions' there.

- Steph

----- Original Message ----- From: "Jeremy Johnstone" <[email protected]>
To: "Andrew Mather" <[email protected]>
Cc: <[email protected]>
Sent: Monday, February 20, 2006 3:08 AM
Subject: Re: [PHP-DEV] PHP / C++ Interaction


On 2/13/06, Andrew Mather <[email protected]> wrote:
- can I talk to a C++ library from PHP, either directly or via a PHP/C extension?
You could just have the PHP extension written in C++. There is no requirement of PHP extensions to be written in C, you just need to make sure you properly wrap the C parts inherited from the core w/ an extern C block. I have wrapped more than a handful of C++ libraries at my day job in into PHP extensions using more or less the following approach. Here's a rough overview of the changes needed (this list might not be complete, but should be close). config.m4 changes --------------------------------- I set the the compiler environment variables (aka CC and CXX) to both use g++. Not sure if this is necessary. In your included libraries (aka PHP_ADD_LIBRARY_WITH_PATH) make sure you include libstdc++ your source file changes ----------------------------------- Wrap the following in an extern "C" like shown below: extern "C" { #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_your_extension_name.h" } And later on further down wrap the following: extern "C" { #ifdef COMPILE_DL_YOUR_EXTENSION_NAME ZEND_GET_MODULE(your_extension_name) #endif } As best as I can remember, that is all that is necessary to write a C++ PHP Extension instead of a C one. Brighter minds on this list might know better if I am forgetting something, but I remember it being very minimal changes so I think the above is it. -Jeremy -- --------------------------- Jeremy Johnstone http://www.jeremyjohnstone.com [email protected]

Thread (5 messages)

« previous php.internals (#21947) next »