php調用c語言編寫的so動態庫


PHP除了使用擴展庫的方式調用c函數,還可以通過socket通信的方式。這里介紹前者。 

第一步: 環境搭建

1. 先看本機是否已經安裝了較低版本的php
   #find /usr -name "php"
   或者rpm -aq | grep php
   如果存在,就使用rpm命令等方式卸掉
2. php源碼安裝,configure的參數如下
#./configure  --prefix=/usr/local/php   --with-mysql --with-mysqli  --enable-so  --with-pdo-mysq=/var/lib/mysql  --with-apxs2=/usr/local/apache/bin/apxs --with-zlib --enable-zip --enable-mbstring
3. 如果已經安裝了兩個版本的php,使用apache函數,確定使用的是哪個版本的php

<html>
<body>
<?php
echo "Hello World";
phpinfo();
?>
</body>
</html>

4. 如果已經安裝了兩個版本的php,使用php查看版本號或確定php.ini的配置文件 
#/usr/local/php/bin/php -v
#/usr/local/php/bin/php -i | grep php.ini

第二步: 用C生成so文件

#vim hello.c
int cc_add(int a, int b)
{
    return a + b;
}
# gcc -O -c -fPIC -o hello.o hello.c                      // -fPIC:是指生成的動態庫與位置無關
# gcc -shared -o libhello.so hello.o                     // -shared:是指明生成動態鏈接庫
# cp libhello.so /usr/local/lib      // 把生成的鏈接庫放到指定的地址
# echo /usr/local/lib > /etc/ld.so.conf.d/local.conf       //  把庫地址寫入到配置文件中
# /sbin/ldconfig                // 用此命令,使剛才寫的配置文件生效

以下是測試so文件

#include <stdio.h>
int main()
{
    int a = 4, b = 6;
    printf("%d\n", cc_add(a,b));
    return 0;
}
#gcc -o hellotest -lhello hellotest.c                // 編譯測試文件,生成測試程序
#./hellotest           // 運行測試程序

第三步: 制作PHP模塊(外部模塊)

1. 然后通過下面的命令用ext_skel腳本建立一個名為 hello 的模塊:
#cd php-5.3.6/ext
#./ext_skel --extname=hello

2. 執行該命令之后它會提示你應當用什么命令來編譯模塊,可惜那是將模塊集成到php內部的編譯方法。
如果要編譯成可動態加載的 php_hello.so,方法要更為簡單。
#cd hello
首先編輯 config.m4 文件,去掉注釋(注釋符號為 dnl 。)
# vim config.m4
PHP_ARG_ENABLE(hello, whether to enable hello support,
dnl Make sure that the comment is aligned:
[  --enable-hello           Enable hello support])

3. 然后執行 phpize 程序,生成configure腳本:
#phpize

4. 打開 php_hello.h,在 PHP_FUNCTION(confirm_hello_compiled); 之下加入函數聲明:
PHP_FUNCTION(confirm_hello_compiled);   /* For testing, remove later. */
PHP_FUNCTION(hello_add);

5. 打開 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)
{
    long int a, b;
    long int result;
 
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &a, &b) == FAILURE) {
        return;
    }
    result = cc_add(a, b);
 
    RETURN_LONG(result);
}

6.配置安裝 
#./configure --with-php-config=/usr/local/php/bin/php-config 
#make LDFLAGS=-lhello
#make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
#cp modules/hello.so /usr/lib/php/modules

#vim /etc/php5/apache2/php.ini

enable_dl = Off; //允許dl()動態加載so擴展功能enable_dl = On

# /etc/init.d/httpd restart
然后在 /var/www/html 下建立一個 hello.php 文件,內容如下:

<html>
<body>
<?php
    dl("hello.so");
    echo hello_add(4, 6);
?>
</body>
</html>

然后在瀏覽器中打開hello.php文件,如果顯示10,則說明函數調用成功了。

第四步. 制作PHP模塊(內部模塊)

另外可以在apache重啟的時候讓我們的so庫直接動態編譯進php5,就像linux的insmod hello.ko模塊一樣,不用dl加載也不用重新編譯php,就可以直接使用so的函數了,步驟如下:

# vim /etc/php5/apache2/php.ini
enable_dl = Off
extension=hello.so
# /etc/init.d/httpd restart

[注意,這種方式只適合hello.so庫內所有功能代碼已經全部調試ok,如果還處在調試期間,那么需要采用上面的dl強制加載的方式]

代碼如下:
<html>
<body>
<?php
    echo hello_add(4, 6);
?>
</body>
</html>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM