Home >Backend Development >PHP Tutorial >How to use C to extend PHP (packaged into so) under Linux

How to use C to extend PHP (packaged into so) under Linux

WBOY
WBOYOriginal
2016-08-08 09:21:271323browse

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.bz2
Note 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

4. Modify the configuration file /test/config.m4

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])

If you want to use To compile C++, rename test.c to test.cpp and add it to config.m4
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 a 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 custom function code in test.c/cpp:

(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[] */
};

(3) Finally, add the code of the testFunc function at the end of the file. You can also 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 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:

phpize
may 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.

Then, solve it by the following method:
yum install autoconf213
export PHP_AUTOC/bin/autoconf-2.13
9. Go to the /root/php-5.6.9/ext/test/ directory and generate the configuration:
./configure --with-php-c/local/php/bin/php-config

10. Compile and generate the .so extension file
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

Then start again from step 7

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"

13. Restart the PHP service
service php-fpm restart

14. Use C++ extension functions in PHP

<?php 
$concat_str = testFunc("concat1","concat2");
echo $concat_str;
?>

Note: If an error occurs when using the extension 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 due to the difference between the php source code package version of the compiled .so file and the currently used php version. You need to download the current version of the php source code package and recompile the extension

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn