Ubuntu64位系統xampp環境編譯32位php擴充庫

來源:互聯網
上載者:User

Ubuntu64位系統xampp環境編譯32位php擴充庫

由於項目需要,需要php調用c語言的庫,由於環境系統是64位,但是php卻是32位,因此需要編譯出32位的庫,本文在之前的文章Ubuntu 下php調用c語言.so檔案基礎上修改完成。

首先寫一個php模組(php extension),在php中調用該模組內的函數,再通過該模組來調用so中的函數。

首先做一個簡單的so檔案:

/**
 * 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;
}

然後將它編譯成.so檔案並放到系統中:

$ 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

寫段小程式來驗證其正確性:

/**
 * 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;
}

編譯並執行:

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

經測試。在Ubuntu12.04上無法通過編譯,但在Ubuntu14.10上可以編譯,在Centos下正常編譯,見,但是不影響後面的使用

 

下面我們製作PHP模組。由於基於xampp,所以php5-dev不裝,直接使用/opt/lampp/bin目錄下的命令即可。

然後下載php原始碼。我使用的是php-5.2.3.tar.gz,解壓縮。

$ 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

然後通過下面的命令建立一個名為 hello 的模組。

$ ./ext_skel --extname=hello

執行該命令之後它會提示你應當用什麼命令來編譯模組,可惜那是將模組整合到php內部的編譯方法。如果要編譯成可動態載入的 php_hello.so,方法要更為簡單。

$ cd hello

首先編輯 config.m4 檔案,去掉第16行和第18行的注釋(注釋符號為 dnl )

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])

然後執行 phpize5 程式,產生configure指令碼:

$ /opt/lampp/bin/phpize

然後開啟 php_hello.h,在 PHP_FUNCTION(confirm_hello_compiled); 之下加入函式宣告:

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

開啟 hello.c,在 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[] */
};

然後在 hello.c 的最末尾書寫hello_add函數的內容:

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

儲存退出,編譯並安裝:

$ CFLAGS=-m32 CPPFLAGS=-m32 CCASFLAGS=-m32 ./configure --with-php-config=/opt/lampp/bin/php-config
$ make LDFLAGS=-lhello
$ make test (測試是否正常安裝)
$ sudo make install 此命令會將so放在php的擴充檔案去

編輯php的設定檔載入so庫

$ vim /opt/lampp/etc/php.ini
找到extension部分,加入extension=hello.so,重啟xampp
$ /opt/lampp/lampp restart

 

然後在 /var/www/ 下建立一個 hello.php 檔案,內容如下:

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

然後在瀏覽器中開啟hello.php檔案,如果顯示7,則說明函數調用成功了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.