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.

9
  • Possible reading suggestion. Commented Aug 30, 2011 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 Commented Aug 30, 2011 at 14:09
  • possible duplicate of Extending PHP with C++? Commented Aug 30, 2011 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. Commented Aug 30, 2011 at 14:10
  • yes i do have access to the source file: .cpp and .h file. how to create a bridging code? Commented Aug 30, 2011 at 14:15

1 Answer 1

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.

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

3 Comments

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?
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 ?
No. PHP is interpreted. C is native. If you want to do it this way, see @therefromhere's comment.

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.