Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling C/C++ library function from PHP [closed]

Tags:

We have a PHP web app running on a customer's machine. For an update, we have a bit of code in C that we'd like to include as a native opaque library along with the PHP web app.

How does one go about calling a C/C++ lib. function from PHP?

It cannot be assumed that the PHP app, called by the web server, has any sort of permission to call an exec(), eval(), or system() type of function to execute a C wrapper driver which in turn uses the C/C++ library, so it would need to be a direct C library use from within the PHP code.

like image 519
Anon. BlackBerry Developer Avatar asked Mar 19 '10 17:03

Anon. BlackBerry Developer


People also ask

Can you use C with PHP?

PHP will not run your C code, even though they have similar syntaxes. The PHP exec function will execute a command similar to how it is done in a shell or command prompt. You could exec gcc -o myc myc.

Can PHP call C++?

It cannot be assumed that the PHP app, called by the web server, has any sort of permission to call an exec(), eval(), or system() type of function to execute a C wrapper driver which in turn uses the C/C++ library, so it would need to be a direct C library use from within the PHP code.

How do you call a function in a shared library?

Put your main function's prototype in a . h file and include it in both your main and dynamic library code. With GCC, simply compile your main program with the -rdynamic flag. Once loaded, your library will be able to call the function from the main program.

How will you call C functions from C ++ and vice versa?

Just declare the C function extern "C" (in your C++ code) and call it (from your C or C++ code). For example: // C++ code. extern "C" void f(int); // one way.


1 Answers

Take a look at some of the Zend tutorials on Extension writing, this one in particular "Wrapping C++ Classes in a PHP Extension"

like image 112
St. John Johnson Avatar answered Nov 01 '22 19:11

St. John Johnson