Reputation: 3077
I have created a PHP Extension in C but i would like to provide all the functionality of my program in my own C++ Dynamic Library (which i will be programming in Xcode).
My question is how do i link against (& use) the functions in my c++ dynlib in my php extension (which will act as a wrapper for my c++ library). What would i need to modify in config.m4 to be able to link against my c++ library?
Upvotes: 2
Views: 1030
Reputation: 31435
Write a C wrapper interface to your C++ library then make the PHP Extension for that.
For a C interface you declare your classes as "struct" even if they are C++ classes with private methods. You don't expose the detail anyway, you use only a forward declaration.
All the public methods are exposed through free functions that take a pointer, and you create instances through a Create methods and destroy them with a Destroy method.
So then you are interacting essentially with a "C library" but the implementation is in C++.
Note that you should put:
#ifdef __cplusplus
extern "C" {
#endif
at the top of your headers (before the methods but after the include guards) and
#ifdef __cplusplus
}
#endif
at the end of them (after the methods but before the include guards)
As you will have to actually build your wrapper library with a C++ compiler because it will be implemented by calling the C++ functions in your library.
Note you can make your C wrapper either a new library that uses the other one, or part of the same library.
The alternative is to use the PHP wrapper macros, which essentially also creates the bindings but does much of the work for you. See
http://devzone.zend.com/1435/wrapping-c-classes-in-a-php-extension/
This will also show you what to do with the config.m4
file.
Upvotes: 2