The content shared with you in this article is to use php to call the code in the so library file. It has a certain reference value. Friends in need can refer to it
Original author charlee, original link http://tech.idv2.com/2007/07/06/use-local-so-in-php/
A certain function is compiled into the so file, so how to call it through php? One method is to write a php module (php extension), call the functions in the module in php,and then use the module to call the functions in so . Here is a simple example using Fedora Core 6 as the operating system.
First make a simple so file:
1 2 3 4 5 6 7 8 9 10 11 |
|
Then compile it into a .so file and put it in the system:
1 2 3 4 5 6 |
|
Write paragraph Mini program to verify its correctness:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Compile and execute:
1 2 3 |
|
OK, let’s make the PHP module. First, make sure you have installed the php-devel package. If not, please find it from the installation CD. Then download the php source code. I am using php-5.2.3.tar.gz, decompressed.
1 2 |
|
Then create a module named hello through the following command.
1 |
|
After executing this command, it will prompt you what command should be used to compile the module. Unfortunately, that is the compilation method that integrates the module into PHP. If you want to compile it into a dynamically loadable php_hello.so, the method is simpler.
1 |
|
First edit the config.m4 file and remove the comments on lines 16 and 18 (the comment symbol is dnl.)
1 2 3 |
|
Then execute the phpize program to generate the configure script:
1 |
|
Then open php_hello.h and add the function declaration under PHP_FUNCTION(confirm_hello_compiled);:
1 2 |
|
Open hello.c and add the following content under PHP_FE(confirm_hello_compiled, NULL).
1 |
|
Then write the content of hello_add function at the end of hello.c:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Save and exit, compile and install:
1 2 3 4 |
|
Then in /var/www/html Create a hello.php file with the following content:
1 2 3 4 |
|
Then open the hello.php file in the browser. If 7 is displayed, the function call is successful.
Linux knowledge supplement:
Due to using Editplus, the default c language source program is saved as .cpp, and then I compile it with gcc under cygwin. As a result, some functions always appear undeclared, but These functions are all standard system calls. Why?
I was depressed about this problem all afternoon and night. Later, I didn’t know why I was asked to try to change the source program .cpp to .c, and the compilation passed smoothly.
Later I learned on the Internet that under Linux, gcc is used to compile .c files, and c files need to use the g command to compile the c source program.
Related recommendations:
Use PHP to obtain visitor IP, regional location, browser and source page information
PHP uses PHPstorm Detailed explanation of the automatic prompt function
The above is the detailed content of Use php to call the code in the so library file. For more information, please follow other related articles on the PHP Chinese website!