自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux gcc編譯寫(xiě)段小程序來(lái)驗(yàn)證其正確性

運(yùn)維 系統(tǒng)運(yùn)維
這個(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++源程序。

特別值得一提的是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)容如下:

  1. <?php 
  2. dl("hello.so");  
  3. echo hello_add(3, 4);  
  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++源程序。

【編輯推薦】

  1. 描述概括Linux GCC穩(wěn)定性的要求
  2. 暢談對(duì)Linux GCC 4.4的理解
  3. 詳細(xì)介紹Linux GCC系統(tǒng)靜態(tài)鏈接
  4. linux gcc版本升級(jí)了
  5. 細(xì)談linux gcc的概念及其參數(shù)
責(zé)任編輯:佚名 來(lái)源: CSDN
相關(guān)推薦

2017-06-05 16:17:50

深度學(xué)習(xí)算法神經(jīng)網(wǎng)絡(luò)

2011-04-19 09:41:22

數(shù)據(jù)庫(kù)

2013-06-24 15:32:00

c++GCC

2015-07-06 14:54:19

Spark計(jì)算正確性Hadoop

2010-02-26 13:43:36

Linux gcc

2011-01-06 11:36:00

linuxGCC編譯器

2024-01-23 11:22:53

谷歌大語(yǔ)言模型AI

2024-01-06 08:10:08

ChatGPT-4人工智能知識(shí)圖譜

2023-10-26 07:54:27

JCStress工具

2018-12-18 17:45:59

數(shù)據(jù)庫(kù)數(shù)據(jù)庫(kù)安全

2009-11-30 13:27:12

Visual Stud

2010-06-04 17:48:20

Linux編程工具

2021-05-24 08:24:48

Linux運(yùn)維Linux系統(tǒng)

2010-01-04 10:06:56

Ubuntu gcc

2022-05-18 07:58:21

Linux程序編譯代碼

2021-10-22 15:31:29

工具代碼開(kāi)發(fā)

2023-11-26 18:31:41

Linux信號(hào)

2024-10-24 16:38:30

測(cè)試線(xiàn)程

2009-06-15 14:00:44

Java小程序驗(yàn)證

2010-02-05 11:00:33

Ubuntu GCC
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)