Linux gcc編譯寫(xiě)段小程序來(lái)驗(yàn)證其正確性
特別值得一提的是Linux gcc編譯有很多值得學(xué)習(xí)的地方,這里我們主要介紹Linux gcc編譯,包括介紹Linux gcc編譯等方面。某個(gè)功能被編譯到so文件中,那么如何通過(guò)php來(lái)調(diào)用它?一個(gè)方法是寫(xiě)一個(gè)php模塊(php extension),在php中調(diào)用該模塊內(nèi)的函數(shù),再通過(guò)該模塊來(lái)調(diào)用so中的函數(shù)。下面做一個(gè)簡(jiǎn)單的例子,使用的操作系統(tǒng)是Fedora Core 6。
首先做一個(gè)簡(jiǎn)單的so文件: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 }
然后將它Linux gcc編譯成.so文件并放到系統(tǒng)中:1 $ gcc -O -c -fPIC -o hello.o hello.c2 $ gcc -shared -o libhello.so hello.o3 $ su4 # echo /usr/local/lib > /etc/ld.so.conf.d/local.conf5 # cp libhello.so /usr/local/lib6 # /sbin/ldconfig
寫(xiě)段小程序來(lái)驗(yàn)證其正確性: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 }
Linux gcc編譯并執(zhí)行:$ gcc -o hellotest -lhello hellotest.c$ ./hellotest3 + 4 = 7OK,下面我們來(lái)制作PHP模塊。首先確保你安裝了 php-devel 包,沒(méi)有的話(huà)請(qǐng)自行從安裝光盤(pán)上找。然后下載php源代碼。我使用的是php-5.2.3.tar.gz,解壓縮。
$ tar xzvf php-5.2.3.tar.gz$ cd php-5.2.3/ext然后通過(guò)下面的命令建立一個(gè)名為 hello 的模塊。$ ./ext_skel --extname=hello執(zhí)行該命令之后它會(huì)提示你應(yīng)當(dāng)用什么命令來(lái)Linux gcc編譯模塊,可惜那是將模塊集成到php內(nèi)部的Linux gcc編譯方法。如果要Linux gcc編譯成可動(dòng)態(tài)加載的 php_hello.so,方法要更為簡(jiǎn)單。
$ cd hello首先編輯 config.m4 文件,去掉第16行和第18行的注釋?zhuān)ㄗ⑨尫?hào)為 dnl 。)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])然后執(zhí)行 phpize 程序,生成configure腳本:
$ phpize然后打開(kāi) php_hello.h,在 PHP_FUNCTION(confirm_hello_compiled); 之下加入函數(shù)聲明:1PHP_FUNCTION(confirm_hello_compiled); /**//* For testing, remove later. */2 PHP_FUNCTION(hello_add);打開(kāi) hello.c,在 PHP_FE(confirm_hello_compiled, NULL) 下方加入以下內(nèi)容。
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 };然后在 hello.c 的最末尾書(shū)寫(xiě)hello_add函數(shù)的內(nèi)容:
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 }保存退出,Linux gcc編譯并安裝:$ ./configure $ make LDFLAGS=-lhello $ su # cp modules/hello.so /usr/lib/php/modules
然后在 /var/www/html 下建立一個(gè) hello.php 文件,內(nèi)容如下:
- <?php
- dl("hello.so");
- echo hello_add(3, 4);
- ?>
然后在瀏覽器中打開(kāi)hello.php文件,如果顯示7,則說(shuō)明函數(shù)調(diào)用成功了。
Linux知識(shí)補(bǔ)充:由于使用Editplus,默認(rèn)c語(yǔ)言源程序保存為.cpp,然后我就在cygwin下用Linux gcc編譯進(jìn)行,結(jié)果總是出現(xiàn)一些函數(shù)undeclared,但是這些函數(shù)都是一些標(biāo)準(zhǔn)的系統(tǒng)調(diào)用,為何呢?
這個(gè)問(wèn)題郁悶了一下午和一晚上,后來(lái)不知道為什么指使我試著把源程序.cpp改成.c,結(jié)果Linux gcc編譯順利通過(guò)。后來(lái)在網(wǎng)上得知,在Linux下,Linux gcc編譯是用來(lái)編譯.c文件的,而c++文件則要用g++命令來(lái)編譯c++源程序。
【編輯推薦】