Example of calling the so file in PHP

Source: Internet
Author: User

How can I use PHP to call a function compiled into the so file? One method is to write a PHP module (PhP extension), call the functions in this module in PHP, and then call the functions in so through this module. The following is a simple example. The operating system is Fedora Core 6.

 

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));    return 0;}

Compile and execute:

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

OK. Let's create the PHP module. First, make sure that you have installed the PHP-devel package. If not, find the package on the installation CD. Then download the PHP source code. I use php-5.2.3.tar.gz for decompression.

$ tar xzvf php-5.2.3.tar.gz$ cd php-5.2.3/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 phpize program to generate the configure script:

$ phpize

Then open php_hello.h and add the function declaration in php_function (confirm_hello_compiled:

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

Open hello. C and add the following content under php_fe (confirm_hello_compiled, null.

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){    long int a, b;    long int 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$ su# cp modules/hello.so /usr/lib/php/modules

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

<?php    dl("hello.so");    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.