Using PHP to invoke code in the so library file

Source: Internet
Author: User
Tags php source code

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

If a feature is compiled into the so file, then how to invoke it through PHP. One way is to write a PHP module (PHP extension), call the function in PHP , and then use the module to call the function in so. Here's a simple example of using the OS 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 {
return a + B;
11}

It is then compiled into a. So file and placed in the system:


1 ___fckpd___1nbsp;gcc-o-c-fpic-o hello.o hello.c
2 ___fckpd___1nbsp;gcc-shared-o libhello.so hello.o
3 ___fckpd___1nbsp;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;
printf ("%d +%d =%d/n", A, B, Hello_add (a,b));
return 0;
12} Compile and execute:
$ gcc-o Hellotest-lhello hellotest.c
$/hellotest
3 + 4 = 7

OK, let's Make a PHP module below. First make sure you install the Php-devel package, if not, please find it by yourself from the installation CD. Then download the PHP source code. I'm using php-5.2.3.tar.gz, decompression.

$ tar xzvf php-5.2.3.tar.gz
$ cd Php-5.2.3/ext

The following command is then used to create a module called Hello.

$./ext_skel--extname=hello

After executing this command, it prompts you what command to use to compile the module, but it is a compilation method that integrates the module into the PHP interior. The method is simpler if you want to compile into a php_hello.so that can be dynamically loaded.

$ cd Hello

First edit the Config.m4 file and remove the comments from lines 16th and 18th (the annotation symbol is DNL.) )


1 16:php_arg_enable (Hello, whether to ENABLE hello support,
2 17:dnl Make sure this comment is aligned:
3: [--enable-hello enable hello support])

Then execute the PHPIZE program to generate the Configure script:

$ phpize

Then open php_hello.h, in php_function (confirm_hello_compiled); Add function declaration below:


1php_function (confirm_hello_compiled); /**//* for testing, remove later. */
2 php_function (Hello_add); Open hello.c and add the following below 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 is the last line in hello_functions[] * *
5};

Then write the contents of the Hello_add function at the very end of the hello.c:

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.