logansama Posted January 8, 2009 Share Posted January 8, 2009 1st: From teh sticky it seems people are strict about what is allowed here, but from what i can define this is best place to have this post - if not, indicate where and i will move the post. I have not personally built php extensions but i have studied the process it goes through. I was instructed to determine (for a project i have just been thrown into) if it is at all possible to build php extensions using either external .so (built form c++ files) or actually using c++ (cpp) files as implementation(s) instead of C files? So far from all the "well explained" tutorials i have found that the default files generated by calling the ext_skel is basically for C code. But like i said we have c++ code we would like to build the extension implementations with. What i have determined so far: 1) One can apparently use PHP_ARG_WITH in the config.m4 which will allow you to specify an external .so file when calling 'configure'. But how this would effect other files in the skel setup i am not aware of. For example: does that mean the default C implementation file is left as is? 2) through online examples i have found that apparently cpp files may be used: http://www.mail-archive.com/internals@lists.php.net/msg24298.html resolved on last reply http://cvs.php.net/viewvc.cgi/pecl/rar/config.m4?revision=1.6&view=markup For all purposes i hope to test out the above to points today, but i must wait for my station to be loaded with linux later this morning before i can try. All i ask for is some input from those who have knowledge in this area, and perhaps a nudge into the right direction of testing/research. Thank you. Quote Link to comment Share on other sites More sharing options...
logansama Posted January 8, 2009 Author Share Posted January 8, 2009 Ok, i have just been informed that i must focus my attention on how to build a PHP extension in linux using external libraries (.so). Thus using the PHP_ARG_WITH in the config.m4 file. My linux box is estimated to arrive in 1 hour. Will try and test then. Any input about the external library version would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
corbin Posted January 8, 2009 Share Posted January 8, 2009 You should theoretically be able to write a PHP extension in C++. Any reason why C won't suffice? Do you want to do it OOP or something? (I prefer C++ over C, just so you know I'm not a C fanboy ;p.) Quote Link to comment Share on other sites More sharing options...
logansama Posted January 9, 2009 Author Share Posted January 9, 2009 An engine for a project has already been created using c++ and it would be the death of the developer if he would be told to redo it in C. what exactly has been done in the c++ code i am not wholly aware of. I have been given a link to a script called Swig which does allow you to create a suitable wrapper of your cpp code. Does most of the code adapting and macro replacing stuff for you. Then you just compile the wrapped code into a library (.so) and use that in php. Problem is that this script requires a list of includes from php and i cannot figure out how to provide the includes. Swig tut: http://www.swig.org/Doc1.3/Php.html#Php_nn2_6 Quote Link to comment Share on other sites More sharing options...
logansama Posted January 12, 2009 Author Share Posted January 12, 2009 If you get the .cpp and header file's structure correct and specify correct php include files then the Swig framework compiles correctly without error. Seems to do all correctly. Now i just need to work out how the hell to correctly load the generated .so file for use in php! Quote Link to comment Share on other sites More sharing options...
jason_mc Posted July 19, 2010 Share Posted July 19, 2010 If you get the .cpp and header file's structure correct and specify correct php include files then the Swig framework compiles correctly without error. Seems to do all correctly. Now i just need to work out how the hell to correctly load the generated .so file for use in php! Did you figure it out? I am having the same problem of not being able to link the .so file into php thanks, Jason here is my small test class: I can’t seem to get my c++ class working in PHP. Does anyone have a simple example I could follow? swig –php –c++ example.i gcc `php-config --includes` -fpic -c example_wrap.cpp gcc -shared example_wrap.o -o example.so then I add the extension to my php.ini file: extension=/root/example/example.so my test example.php file: include("example.php"); echo “<br>Before SWIG<br>”; echo example::function1(); //should return “Function1 Working” echo “<br>After SWIG<br>”; The PHP file will stop/fail at the function1() function The example.cpp class will work from the prompt but not in the php file. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.