3

I've created a library.a that contains a .cpp and .h files with a lot of classes, nested classes and methods. I would like to include this static library inside a php example and try to work with it. I would like to mention that I am new to php. I've tested my libray.a inside a test.cpp file and it works. How can I create a test.php and test my library.a?If it;s possible please send me some examples.

I would like to mention that i am working in ubuntu. g++ was my compiler.

Thx for advices! Appreciate!

EDIT: I WOULD LIKE TO MENTION THE FACT THAT I DON'T WANT TO EXPOSE MY .CPP CODE. I JUST WANT TO USE MY .H AND .A FILES.

sunset
  • 1,359
  • 5
  • 22
  • 35
  • Possible [reading suggestion](http://devzone.zend.com/article/4486). – Kerrek SB Aug 30 '11 at 14:05
  • i don't want to use my .cpp and .h files. i want to use my .a library. i don;t want to expose my .cpp code – sunset Aug 30 '11 at 14:09
  • possible duplicate of [Extending PHP with C++?](http://stackoverflow.com/questions/1110682/extending-php-with-c) – John Carter Aug 30 '11 at 14:10
  • You could doubly wrap the library in another C++ wrapper... but why, don't you have access to the source? You have to write some bridging code one way or another, you can't just add a random library to PHP and magically expect PHP to provide an interface to it. – Kerrek SB Aug 30 '11 at 14:10
  • yes i do have access to the source file: .cpp and .h file. how to create a bridging code? – sunset Aug 30 '11 at 14:15
  • @sunset, yes this is fine, when you write your PHP extension you'd just call the library functions. One issue you'll have is that you'll need to compile your static library with `-fPIC` (on gcc), and you'll have the usual issues associated with compiler compatibility when distributing C++ libraries. BTW the reason you need `-fPIC` is because PHP extensions are dynamic libraries. – John Carter Aug 30 '11 at 14:15
  • See my answer here: http://stackoverflow.com/questions/1110682/extending-php-with-c/1966341#1966341 – John Carter Aug 30 '11 at 14:16
  • @therefromhere; i've compiled my library using g++ -c -static -L -lb library.cpp -o library.o and the using : ar rcs library.a library.o. is it ok? I would like to mention that i've never used php. i am new with it. can you please write step by step how should i work? is there a link with an example based on my quesion? – sunset Aug 30 '11 at 14:20
  • Follow the tutorials in the question I linked and you should be fine. – John Carter Aug 30 '11 at 15:49

1 Answers1

1

An .a file is not a self-executable library. It is static object code. It cannot run by itself.

PHP doesn't have loaders. It can't load a .a file, neither your very own operating system can.

An .a file needs to be accompanied by the appropriate headers (.h files).

If you want to use native code within PHP, you must use PHP's interfaces. See, just like anything built with C/C++, PHP has it's own definition of what a string (or most data types) look like.

In short, you have two options:

  • use PHP's headers and interface your code directly with PHP
  • use a library wrapper which connects your calls to PHP
  • make your library into an executable and call it with PHP*

*PHP has plenty IPC methods, so this is actually quite feasible.

Christian
  • 27,509
  • 17
  • 111
  • 155
  • if i want to use the first option can u please explain how to use the php headers? can i include in .php the .h and .a that i have? – sunset Aug 30 '11 at 14:26
  • sorry for my stupid questions:). Can you please extend your answer? i am new to php. if i create a dynamic library i won;t need the .h file ? – sunset Aug 30 '11 at 14:27
  • No. PHP **is interpreted**. C **is native**. If you want to do it this way, see @therefromhere's comment. – Christian Aug 30 '11 at 14:28