Re: 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)