Php In Ubuntu calls the C language. so file

Source: Internet
Author: User

Php In Ubuntu calls the C language. so file

Php In Ubuntu calls the C language. so file. Write a php module (php extension), call the functions in this module in php, and then call the functions in so through this module.

First, make a simple so file:

/**
 * hello.c
 * To compile, use following commands:
 *   gcc -O -c -fPIC -o hello.o hello.c 
 *   gcc -shared -o libhello.so hello.o
 */
 
int hello_add(int a, int b)
{
    return a + b;
}

Compile it into a. so file and put it in the system:

$ gcc -O -c -fPIC -o hello.o hello.c
$ gcc -shared -o libhello.so hello.o
$ su
# echo /usr/local/lib > /etc/ld.so.conf.d/local.conf  
# cp libhello.so /usr/local/lib
# /sbin/ldconfig

Write a small program to verify its correctness:

/**
 * hellotest.c
 * To compile, use following commands:
 *   gcc -o hellotest -lhello hellotest.c
 */
#include <stdio.h>
int main()
{
    int a = 3, b = 4;
    printf("%d + %d = %d\n", a, b, hello_add(a,b));
    return0;
}

Compile and execute:

$ gcc -o hellotest -lhello hellotest.c
$ ./hellotest
3 + 4 = 7

Tested. It cannot be compiled on Ubuntu. It can be compiled normally in CentOS. See this document, but it does not affect subsequent use.

 

Next we will create the PHP module. First make sure that the php5-dev package is installed

$ apt-get install php5-dev

Then download the php source code. I use php-5.2.3.tar.gz for decompression.

$ wget http://eduunix.ccut.edu.cn/index2/php/php/php-5.3.6.tar.gz
$ tar xzvf php-5.3.6.tar.gz
$ cd php-5.3.6/ext

Run the following command to create a module named hello.

$ ./ext_skel --extname=hello

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 comments (the annotation symbol is dnl) from lines 2 and 3)

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

Then run the phpize5 program to generate the configure script:

$ phpize5

Then open php_hello.hPHP_FUNCTION(confirm_hello_compiled);Add the function declaration below:

PHP_FUNCTION(confirm_hello_compiled);   /* For testing, remove later. */
PHP_FUNCTION(hello_add);

Open hello. c and goPHP_FE(confirm_hello_compiled, NULL)Add the following content below.

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 write the hello_add function at the end of hello. c:

PHP_FUNCTION(hello_add)
{
    longint a, b;
    longint result;
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &a, &b) == FAILURE) {
        return;
}
    result = hello_add(a, b);
    RETURN_LONG(result);
}

Save and exit, compile and install:

$ ./configure
$ make LDFLAGS=-lhello
$sudo make install This command willsoPut inphpExtension file

Edit the php configuration file to load the so Library

$ ./configure
$ vim /etc/php5/apache2/php.ini
FindextensionPart, addextension=hello.so

 

Create a hello. php file under/var/www/with the following content:

<?php
    echo hello_add(3, 4);
?>

Then open the "hello. php" file in the browser. If "7" is displayed, the function is successfully called.

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.