偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Linux gcc編譯寫段小程序來驗證其正確性

運維 系統(tǒng)運維
這個問題郁悶了一下午和一晚上,后來不知道為什么指使我試著把源程序.cpp改成.c,結(jié)果Linux gcc編譯順利通過。后來在網(wǎng)上得知,在Linux下,Linux gcc編譯是用來編譯.c文件的,而c++文件則要用g++命令來編譯c++源程序。

特別值得一提的是Linux gcc編譯有很多值得學(xué)習(xí)的地方,這里我們主要介紹Linux gcc編譯,包括介紹Linux gcc編譯等方面。某個功能被編譯到so文件中,那么如何通過php來調(diào)用它?一個方法是寫一個php模塊(php extension),在php中調(diào)用該模塊內(nèi)的函數(shù),再通過該模塊來調(diào)用so中的函數(shù)。下面做一個簡單的例子,使用的操作系統(tǒng)是Fedora Core 6。

首先做一個簡單的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

寫段小程序來驗證其正確性: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,下面我們來制作PHP模塊。首先確保你安裝了 php-devel 包,沒有的話請自行從安裝光盤上找。然后下載php源代碼。我使用的是php-5.2.3.tar.gz,解壓縮。

$ tar xzvf php-5.2.3.tar.gz$ cd php-5.2.3/ext然后通過下面的命令建立一個名為 hello 的模塊。$ ./ext_skel --extname=hello執(zhí)行該命令之后它會提示你應(yīng)當(dāng)用什么命令來Linux gcc編譯模塊,可惜那是將模塊集成到php內(nèi)部的Linux gcc編譯方法。如果要Linux gcc編譯成可動態(tài)加載的 php_hello.so,方法要更為簡單。

$ cd hello首先編輯 config.m4 文件,去掉第16行和第18行的注釋(注釋符號為 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然后打開 php_hello.h,在 PHP_FUNCTION(confirm_hello_compiled); 之下加入函數(shù)聲明:1PHP_FUNCTION(confirm_hello_compiled);   /**//* For testing, remove later. */2 PHP_FUNCTION(hello_add);打開 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 的最末尾書寫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 下建立一個 hello.php 文件,內(nèi)容如下:

  1. <?php 
  2. dl("hello.so");  
  3. echo hello_add(3, 4);  
  4. ?> 

然后在瀏覽器中打開hello.php文件,如果顯示7,則說明函數(shù)調(diào)用成功了。

Linux知識補充:由于使用Editplus,默認(rèn)c語言源程序保存為.cpp,然后我就在cygwin下用Linux gcc編譯進行,結(jié)果總是出現(xiàn)一些函數(shù)undeclared,但是這些函數(shù)都是一些標(biāo)準(zhǔn)的系統(tǒng)調(diào)用,為何呢?

這個問題郁悶了一下午和一晚上,后來不知道為什么指使我試著把源程序.cpp改成.c,結(jié)果Linux gcc編譯順利通過。后來在網(wǎng)上得知,在Linux下,Linux gcc編譯是用來編譯.c文件的,而c++文件則要用g++命令來編譯c++源程序。

【編輯推薦】

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

2017-06-05 16:17:50

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

2011-04-19 09:41:22

數(shù)據(jù)庫

2013-06-24 15:32:00

c++GCC

2015-07-06 14:54:19

Spark計算正確性Hadoop

2010-02-26 13:43:36

Linux gcc

2024-01-23 11:22:53

谷歌大語言模型AI

2011-01-06 11:36:00

linuxGCC編譯器

2023-10-26 07:54:27

JCStress工具

2024-01-06 08:10:08

ChatGPT-4人工智能知識圖譜

2018-12-18 17:45:59

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

2009-11-30 13:27:12

Visual Stud

2010-06-04 17:48:20

Linux編程工具

2010-01-04 10:06:56

Ubuntu gcc

2021-05-24 08:24:48

Linux運維Linux系統(tǒng)

2022-05-18 07:58:21

Linux程序編譯代碼

2023-11-26 18:31:41

Linux信號

2021-10-22 15:31:29

工具代碼開發(fā)

2024-10-24 16:38:30

測試線程

2022-06-06 06:10:00

密碼驗證安全

2009-06-15 14:00:44

Java小程序驗證
點贊
收藏

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