Extension written in C++ and undefined symbols

php.internals

Iridium

22 years ago
I've currently got some libraries written in C++ with various class interfaces, I was going to write an interface between them and PHP but got very stuck trying to get the extension to recognize any C++ linked to it at all.. I spent quite a while searching the archives and google before posting this, and found some advice telling me to set EXTRA_LDFLAGS = -lstdc++. This seemed to fix the odd one or two things, I don't remember what now because that was a while ago.. in my config.m4: PHP_NEW_EXTENSION(nalicity, nalicity.c test.cpp, $ext_shared) in test.cpp: void blah() { printf( "\n\nBlah\n\n" ); } in nalicity.c PHP_FUNCTION(testfunction) { blah(); } Doesn't work unless I rename test.cpp to test.c and change the PHP_NEW_EXTENSION line accordingly. It compiles fine, I just get the error: /usr/bin/php: relocation error: /usr/lib/php/extensions/no-debug-non-zts-20020429/../../../../src/php/exts/nalicity/modules/nalicity.so: undefined symbol: blah when I try to run the PHP and load the dl(). As I said above though, works fine as a plain old C file. Any ideas? I've stayed up all night trying to figure it out but have gotten nowhere. (and on an unrelated note, is there any way I can specify an absolute path to dl()? I feel kinda stupid with all those ../..'s in there.) Thanks in advance for any help, Peter.

Vladimir Zidar

22 years ago
When you need to access function compiled from C++ source code, you need to declare it as extern "C", in order to tell the compiler to generate function name that is compatible with C. Otherwise, compiler will generate different name (will mangle it), and C won't be able to find it. So, write extern "C" blah() { ... } in test.cpp On Sun, 2004-05-16 at 05:08, Peter 'iridium' Waller wrote:
> I've currently got some libraries written in C++ with various class > interfaces, I was going to write an interface between them and PHP but > got very stuck trying to get the extension to recognize any C++ linked > to it at all.. > > I spent quite a while searching the archives and google before posting > this, and found some advice telling me to set EXTRA_LDFLAGS = -lstdc++. > This seemed to fix the odd one or two things, I don't remember what now > because that was a while ago.. > > in my config.m4: > > PHP_NEW_EXTENSION(nalicity, nalicity.c test.cpp, $ext_shared) > > in test.cpp: > > void blah() > { > printf( "\n\nBlah\n\n" ); > } > > in nalicity.c > > PHP_FUNCTION(testfunction) > { > blah(); > } > > Doesn't work unless I rename test.cpp to test.c and change the > PHP_NEW_EXTENSION line accordingly. It compiles fine, I just get the error: > > /usr/bin/php: relocation error: > /usr/lib/php/extensions/no-debug-non-zts-20020429/../../../../src/php/exts/nalicity/modules/nalicity.so: > undefined symbol: blah > > when I try to run the PHP and load the dl(). As I said above though, > works fine as a plain old C file. Any ideas? I've stayed up all night > trying to figure it out but have gotten nowhere. > > (and on an unrelated note, is there any way I can specify an absolute > path to dl()? I feel kinda stupid with all those ../..'s in there.) > > Thanks in advance for any help, > > Peter.
-- Vladimir Zidar, Programmer MindNever Foodstuff TRDG. Head Office Branch Tel: +381-63-550161, +385-98-9574261 http://www.mindnever.org, icq: 15414204, mail: mr_W@mindnever.org

Iridium

22 years ago
Thank you muchly. It took considerable trial and error but I got it working. For any poor person that ends up searching this list and comes up to this thread, in the config.m4 file add the lines PHP_REQUIRE_CXX EXTRA_LDFLAGS=-lstdc++ Somewhere. Also make sure that your file ends in the extension .cpp. Peter. Vladimir Zidar wrote:

Asgeir Frimannsson

22 years ago
Peter 'Iridium' Waller wrote:
> I've currently got some libraries written in C++ with various class > interfaces, I was going to write an interface between them and PHP but > got very stuck trying to get the extension to recognize any C++ linked > to it at all..
Not an answer to your question (since it's allready answered), but might be useful for some: I was searching google the other day, and found a great starter-tutorial by Jay Smith on how to write c++ extensions in ZE2. http://bugs.tutorbuddy.com/php5cpp/php5cpp/index.html regards asgeir frimannsson