Home >Backend Development >PHP Tutorial >How to use C to extend PHP (packaged into so) under Linux
This article mainly talks about the differences between packaging C extension .so files under Linux and Windows. For detailed code and configuration plans, please visit another blog: http://blog.csdn.net/maverick1990/article/details /46519045
Steps:
1. Install the PHP environment to the directory /usr/local/php/
2. Download the same version of the PHP source code package and install it to the /root/php-5.6.9/ directory. You can download it from the official website http://www.php.net/downloads.php
Execute the command:
cd /root wget http://us1.php.net/distributions/php-5.6.9.tar.bz2 tar -xf php-5.6.9.tar.bz2Note that some historical versions do not have source code packages, and you need to upgrade PHP to a version that provides source code packages
3. Go to the /php-5.6.9/ext/ directory and use ext_skel to generate the extended skeleton
cd ./php-5.6.9/ext ./ext_skel --extname=test
Cancel the dnl comments in the following two lines:
PHP_ARG_ENABLE(test, whether to enable test support, dnl Make sure that the comment is aligned: [ --enable-test Enable test support])
PHP_REQUIRE_CXX() PHP_ADD_LIBRARY(stdc++, 1, EXTRA_LDFLAGS) PHP_NEW_EXTENSION(test, test.cpp, $ext_shared)
PHP_MINIT_FUNCTION(test); PHP_MSHUTDOWN_FUNCTION(test); PHP_RINIT_FUNCTION(test); PHP_RSHUTDOWN_FUNCTION(test); PHP_MINFO_FUNCTION(test); PHP_FUNCTION(confirm_test_compiled); /* For testing, remove later. */ PHP_FUNCTION(testFunc);
(1) First, introduce the header file used at this location:
#ifdef HAVE_CONFIG_H #include "config.h" #endif #include <stdio.h> #include <string.h> #include <math.h>
#include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_test.h"Otherwise, there will be many function redefinition problems. For details, see another Blog: http://blog.csdn.net/maverick1990/article/details/46786685
(2) Then, add the function entry at this location:
const zend_function_entry test_functions[] = { PHP_FE(confirm_test_compiled, NULL) /* For testing, remove later. */ PHP_FE(testFunc, NULL) PHP_FE_END /* Must be the last line in test_functions[] */ };
PHP_FUNCTION(testFunc) { char *x = NULL; char *y = NULL; int argc = ZEND_NUM_ARGS(); int x_len; int y_len; if (zend_parse_parameters(argc TSRMLS_CC, "ss", &x, &x_len, &y, &y_len) == FAILURE) return; int result_length = x_len + y_len; char* result = (char *) emalloc(result_length + 1); strcpy(result, x); strcat(result, y); RETURN_STRINGL(result, result_length, 0); }
For details on how to write the PHP_FUNCTION function, please see the blog: http://weizhifeng.net/write-php-extension-part2-1.html
7. In /root/php-5.6.9/ext/ In the test/ directory, create a php extension module:
phpizemay need to add the path:
/usr/local/php/bin/phpize
8. Return to the /root/php-5.6.9/ directory and re-establish the configuration required for compilation:
cd ../.. ./buildconf --force
If a similar error occurs:
buildconf: You need autoconf 2.59 or lower to build this version of PHP. You are currently trying to use 2.63 Most distros have separate autoconf 2.13 or 2.59 packages. On Debian/Ubuntu both autoconf2.13 and autoconf2.59 packages exist. Install autoconf2.13 and set the PHP_AUTOCONF env var to autoconf2.13 and try again.
yum install autoconf213 export PHP_AUTOC/bin/autoconf-2.139. Go to the /root/php-5.6.9/ext/test/ directory and generate the configuration:
./configure --with-php-c/local/php/bin/php-config
make make install. If make makes an error, modify the code until the compilation passes.
If there is an error in make install, after modification, you need to clear the currently generated extension modules:
phpize --clean
11. Check whether test.so is generated
After make install is successful, the generation path will be given, for example: /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/test.so
12. Modify php.ini
Modify the /usr/local/php/etc/php.ini file and add the extension path and name:
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/" extension = "teste.so"
service php-fpm restart
14. Use C++ extension functions in PHP
<?php $concat_str = testFunc("concat1","concat2"); echo $concat_str; ?>
PHP Warning PHP Startup Unable to initialize module Module compiled with module API=20121212 PHP compiled with module API=20090626 These options need to match in Unknown on line 0
Copyright statement: This article is an original article by the blogger, No reproduction is allowed without the permission of the blogger.
The above introduces the method of using C to extend PHP (packaged into so) under Linux, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.