Ubuntu64-bit system xampp environment for compiling 32-bit php extension Library

Source: Internet
Author: User

Ubuntu64-bit system xampp environment for compiling 32-bit php extension Library

For the project, php needs to call the C language library. Because the Environment System is 64-bit, but php is 32-bit, a 32-bit library needs to be compiled, in this article, php calls C language in Ubuntu. so file.

First, 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  -m32
 *   gcc -shared -o libhello.so hello.o -m32
 */
 
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 -m32
$ gcc -shared -o libhello.so hello.o -m32
$ 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 -m32
 */
#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 -m32
$ ./hellotest
3 + 4 = 7

Tested. It cannot be compiled on Ubuntu12.04, but it can be compiled on Ubuntu14.10. It can be compiled normally under Centos. See this article, but it does not affect subsequent use.

 

Next we will create the PHP module.Because based on xampp, so the php5-dev is not installed, directly use the command under the/opt/lampp/bin directory.

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:

$ /opt/lampp/bin/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)
{
    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:

$ CFLAGS=-m32 CPPFLAGS=-m32 CCASFLAGS=-m32 ./configure --with-php-config=/opt/lampp/bin/php-config
$ make LDFLAGS=-lhello
$ Make test (test whether the installation is normal)
$ Sudo make install this command will put so in the extension file of php

Edit the php configuration file to load the so Library

$ vim /opt/lampp/etc/php.ini
Find the extension part, add extension = hello. so, and restart xampp.
$ /opt/lampp/lampp restart

 

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.