Use PHP to call the code in the so Library File

Source: Internet
Author: User

Original Author charlee, original link http://tech.idv2.com/2007/07/06/use-local-so-in-php/

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

First, make a simple so file:

 
 1  /**
 2   * hello.c
 3   * To compile, use following commands:
 4   *   gcc -O -c -fPIC -o hello.o hello.c 
 5   *   gcc -shared -o libhello.so hello.o
 6   */
 7  
 8  int hello_add(int a, int b)
 9  {
10      return a + b;
11  }

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


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

Write a small program to verify its correctness:


1 /**//**
2 * hellotest. c
3 * to compile, use following commands:
4 * gcc-O hellotest-lhello hellotest. c
5 */
6 # include <stdio. h>
7 int main ()
8 {
9 int A = 3, B = 4;
10 printf ("% d + % d = % d \ n", a, B, hello_add (a, B ));
11 return 0;
12} 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 .)


1 16:  PHP_ARG_ENABLE(hello, whether to enable hello support,
2 17:  dnl Make sure that the comment is aligned:
3 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:


1php_function (confirm_hello_compiled);/** // * for testing, remove later .*/
2 php_function (hello_add); open hello. C and add the following content under php_fe (confirm_hello_compiled, null.
 
1 zend_function_entry hello_functions[] = {
2     PHP_FE(confirm_hello_compiled,  NULL)       /**//* For testing, remove later. */
3     PHP_FE(hello_add,   NULL)       /**//* For testing, remove later. */
4     {NULL, NULL, NULL}  /**//* Must be the last line in hello_functions[] */
5 };

Then write the hello_add function at the end of Hello. C:


1php_function (hello_add)
2 {
3 long int A, B;
4 long int result;
5
6 if (zend_parse_parameters (zend_num_args () tsrmls_cc, "ll", & A, & B) = failure ){
7 return;
8}
9
10 result = hello_add (A, B );
11
12 return_long (result );
13} 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:

 <?phpdl("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.

Linux knowledge supplement:

Because editplus is used, the C language source program is saved. CPP, and then I will compile it with GCC in cygwin. Some undeclared functions will always appear in the result, but these functions are called by some standard systems. Why?

This problem was depressing for one afternoon and one night. Later I was not sure why I tried to change the source program. cpp to. C, and the compilation was successful. Later, I learned on the Internet that GCC is used to compile the. c file in Linux, while the C ++ file uses the G ++ command to compile the C ++ source program.

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.