Document directory
- Step 1: Build the environment
- Step 2: Use C to generate the so file
- Step 3: Create a PHP module (external module)
- Step 4. Create the PHP module (internal module)
In addition to calling the C function using the extension library, PHP can also use socket communication. Here we will introduce the former.
Step 1: Build the environment
1. Check whether the local machine has installed a lower version of PHP
# Find/usr-name "php"
Or rpm-AQ | grep PHP
If the command exists, run the RPM command to unmount it.
2. install PHP source code. The configure parameters are as follows:
#. /Configure -- prefix =/usr/local/PHP -- With-mysql -- With-mysqli -- enable-so -- With-PDO-mysq =/var/lib/MySQL -- with-apxs2 =/ usr/local/Apache/bin/apxs -- With-zlib -- enable-zip -- enable-mbstring
3. If two versions of PHP have been installed, use the Apache function to determine which version of PHP is used.
4. If two versions of PHP have been installed, use PHP to view the version number or determine the configuration file of PHP. ini.
#/Usr/local/PHP/bin/PHP-V
#/Usr/local/PHP/bin/PHP-I | grep PHP. ini
Step 2: Use C to generate the so file
# Vim hello. CINT cc_add (int A, int B) {return a + B;} # gcc-o-c-FPIC-O hello. O hello. C //-FPIC: The generated dynamic library is not related to the location # gcc-shared-O libhello. so hello. O //-shared: Specifies to generate the dynamic link library # cp libhello. so/usr/local/lib // put the generated Link Library to the specified address # echo/usr/local/lib>/etc/lD. so. conf. d/local. conf // write the database address to the configuration file #/sbin/ldconfig // Use this command to make the configuration file you just wrote take effect
Below is the test so file
# Include <stdio. h> int main () {int A = 4, B = 6; printf ("% d \ n", cc_add (A, B); Return 0 ;} # gcc-O hellotest-lhello hellotest. C // compile the test file and generate the Test Program #. /hellotest // run the test program
Step 3: Create a PHP module (external module)
1. Use the following command to create a module named Hello using the ext_skel script:
# Cd php-5.3.6/EXT
#./Ext_skel -- extname = Hello
2. After executing this command, it will prompt you what command should be used to compile the module. Unfortunately, it is a compilation method that integrates the module into PHP.
To compile php_hello.so that can be dynamically loaded, the method is simpler.
# Cd hello
First, edit the config. M4 file and remove the annotation (the annotation symbol is DNL .)
# Vim config. M4
Php_arg_enable (hello, whether to enable Hello support,
DNL make sure that the comment is aligned:
[-- Enable-Hello enable Hello support])
3. Then run the phpize program to generate the configure script:
# Phpize
4. Open php_hello.h and add the function declaration under php_function (confirm_hello_compiled:
Php_function (confirm_hello_compiled);/* for testing, remove later .*/
Php_function (hello_add );
5. Open hello. C and add the following content under php_fe (confirm_hello_compiled, null.
Zend_function_entry hello_functions [] = {php_fe (confirm_hello_compiled, null)/* for testing, remove later. */php_fe (hello_add, null)/* for testing, remove later. */{null, null, null}/* must be the last line in hello_functions [] */}; then in hello. at the end of C, write the hello_add function: php_function (hello_add) {long int A, B; long int result; If (zend_parse_parameters (zend_num_args () tsrmls_cc, "ll ", & A, & B) = failure) {return;} result = cc_add (a, B); return_long (result );}
6. Configure and install
#./Configure -- With-PHP-Config =/usr/local/PHP/bin/PHP-config
# Make ldflags =-lhello
# Make install
Installing shared extensions:/usr/local/PHP/lib/PHP/extensions/no-debug-non-zts-20090626/
# Cp modules/Hello. So/usr/lib/PHP/modules
# Vim/etc/PhP5/apache2/PHP. ini
Enable_dl = off; // enable_dl = on
#/Etc/init. d/httpd restart
Create a hello. php file under/var/www/html with the following content:
Then open the hello. php file in the browser. If 10 is displayed, the function is successfully called.
Step 4. Create the PHP module (internal module)
In addition, we can let our so library directly and dynamically compile it into PhP5 when Apache is restarted, just like Linux's insmod hello. like the Ko module, you can directly use the so function without DL loading or re-compiling PHP. The steps are as follows:
# Vim/etc/PhP5/apache2/PHP. ini
Enable_dl = off
Extension = Hello. So
#/Etc/init. d/httpd restart
[Note: This method is only suitable for debugging okay for all functional code in the hello. So library. If it is still in the debugging stage, use the above DL Force Loading Method]
The Code is as follows: <HTML> <body> <? PHP echo hello_add (4, 6);?> </Body>