How to use C to expand PHP (packaged as so) in Linux

Source: Internet
Author: User
Tags php download zts
: This article mainly introduces how to use C extension PHP (packaged as so) in Linux. For more information about PHP tutorials, see. This article mainly talks about packaging C extension program. so file in Linux and Windows different, detailed code and configuration solution please participate in another blog: http://blog.csdn.net/maverick1990/article/details/46519045

Steps:

1. install the php environment to the/usr/local/php/directory.

2. download the same version of php source package, install to/root/php-5.6.9/directory, go to the official website http://www.php.net/downloads.php download

Run the following command:

cd /rootwget http://us1.php.net/distributions/php-5.6.9.tar.bz2tar -xf php-5.6.9.tar.bz2
Note: Some Historical versions do not have source code packages. you need to upgrade php to the version that provides the source code 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 of the following two rows:

PHP_ARG_ENABLE(test, whether to enable test support,dnl Make sure that the comment is aligned:[  --enable-test           Enable test support])

To use C ++ for compilation, change test. c to test. cpp and add
PHP_REQUIRE_CXX()    PHP_ADD_LIBRARY(stdc++, 1, EXTRA_LDFLAGS)PHP_NEW_EXTENSION(test, test.cpp, $ext_shared)

5. add code to the php_test.h file and add 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 a lot of function redefinition issues, see another blog: http://blog.csdn.net/maverick1990/article/details/46786685

(2) Then, add the function entry at 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 be the last line in test_functions[] */};

(3) add the code for the testFunc function at the end of the file. you can also define other user-defined 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 PHP_FUNCTION writing method, see the blog: http://weizhifeng.net/write-php-extension-part2-1.html

7. create a php extension module under the/root/php-5.6.9/ext/test/directory:

phpize
You may need to add the following path:
/usr/local/php/bin/phpize

8. back to the/root/php-5.6.9/directory and re-build 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.

Then, solve the problem through the following method:
yum install autoconf213export PHP_AUTOC/bin/autoconf-2.13
9. go to the/root/php-5.6.9/ext/test/directory to generate the configuration:
./configure --with-php-c/local/php/bin/php-config

10. Compile and generate the. so extension file
makemake install
If a make error occurs, modify the code until the code is compiled

If an error occurs in make install, you need to clear the generated extension module after modification:

phpize --clean

Then start from step 1.

11. check whether test. so is generated.

Make install will give the generated path, 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"

13. restart the PHP service.
service php-fpm restart

14. use C ++ extension functions in PHP

 

Note: If an error occurs when using an extended function:
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

This is because the php source package version of the. so file is different from the current php version. you need to download the current version of the php source package and re-compile the extension.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above describes how to use C extension PHP (packaged as so) in Linux, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.