This article is mainly about the use of packaged C extension under Linux. So file and under different, detailed code and configuration scenarios please join another blog: http://blog.csdn.net/maverick1990/article/details/46519045
Steps:
1. Install the PHP environment into the directory/usr/local/php/directory
2. Download the same version of the PHP source package, installed in the/root/php-5.6.9/directory, can go to the official website http://www.php.net/downloads.php download
Execute command:
Cd/rootwget HTTP://US1.PHP.NET/DISTRIBUTIONS/PHP-5.6.9.TAR.BZ2TAR-XF php-5.6.9.tar.bz2
Note that there are some historical versions without source packages that need to be upgraded to PHP to provide a version of the source package
3. To the/php-5.6.9/ext/directory, use Ext_skel to generate the extension skeleton
CD./php-5.6.9/ext./ext_skel--extname=test
4. Modify the configuration file/test/config.m4
Cancel the DNL comment for the following two lines:
Php_arg_enable (test, whether to ENABLE test support,dnl Make sure that the comment is aligned:[ --enable-test Enab Le test support])
If you want to compile with C + +, rename test.c to Test.cpp and add it in CONFIG.M4
Php_require_cxx () php_add_library (stdc++, 1, extra_ldflags) php_new_extension (test, test.cpp, $ext _shared)
5. Add the code to the Php_test.h file, adding the custom function declaration:
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);
6. Add the Custom function code in Test.c/cpp:
(1) First, introduce the header file used in this position:
#ifdef have_config_h#include "Config.h" #endif # include
#include
#include
#include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_test.h"
Otherwise, there will be many problems with function redefinition, see another blog: http://blog.csdn.net/maverick1990/article/details/46786685
(2) Then, add the function entry in this position:
Const Zend_function_entry test_functions[] = {Php_fe (confirm_test_compiled, NULL)/* For testing, remove later. */php_fe ( TestFunc, NULL) php_fe_end/* must is the last line in test_functions[] */};
(3) Finally, add the code for the TestFunc function at the end of the file, or you can define other custom functions here
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 the writing method of php_function function, see blog: http://weizhifeng.net/write-php-extension-part2-1.html
7. In the/root/php-5.6.9/ext/test/directory, build the PHP extension module:
Phpize
You may need to add a path:
/usr/local/php/bin/phpize
8. Go back 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 is currently trying to use 2.63 most distros has 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.
Then, use the following method to resolve:
Yum Install Autoconf213export php_autoc/bin/autoconf-2.13
9. Then to the/root/php-5.6.9/ext/test/directory, build the configuration:
./configure--with-php-c/local/php/bin/php-config
10. Compile and build the. so extension file
Makemake Install
If make makes an error, modify the code until the compilation passes
If make install error, after modification, you need to empty the currently generated extension module:
Phpize--clean
and start again from step 7th.
11. See if Test.so is generating
The build path is given after make install succeeds, 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 to include the extension path and name:
Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/" extension = "teste.so"
13. Restart PHP Service
Service PHP-FPM Restart
14. Using C + + extension functions in PHP
Note: An error occurred when using the extension function:
PHP Warning PHP Startup Unable to initialize module module compiled with module api=20121212 PHP compiled with MoD Ule api=20090626 These options need to match with Unknown on line 0
This is due to the PHP source package version of the. So file is different from the PHP version currently in use, you need to download the current version of the PHP source package, recompile the extension
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The above describes the use of C to extend PHP under Linux (packaged into so), including the content, I hope the PHP tutorial interested in a friend to help.