2

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thats what i've been trying to do. but how do i link against my c++ library using the php extension makefile or config.m4?
You link against it as a C library and only call the C interface functions. So the same as what you did for a C extension, do here.
but, how do i do that in config.m4? like link it? using gcc i'd directly call -lmylib but i'm not sure about it in this circumstance
The link at devzone has an example, including the changes you would make to config.m4 Obviously you will use your own names and not vehicle and car

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.