linux系統下的gcc(gnu c compiler)是gnu推出的功能強大、效能優越的多平台編譯器,是gnu的代表作品之一。gcc是可以在多種硬體平台上編譯出可執行程式的超級編譯器,其執行效率與一般的編譯器相比平均效率要高20%~30%。
gcc 可同時用來編譯 c 程式和 c++ 程式。一般來說,c 編譯器通過原始檔的字尾名來判斷是 c 程式還是 c++ 程式。
在 linux 中,c 原始檔的字尾名為 .c,而 c++ 原始檔的字尾名為 .c 或 .cpp。
但是,gcc 命令只能編譯 c++ 原始檔,而不能自動和 c++ 程式使用的庫連線。
因此,通常使用 g++ 命令來完成 c++ 程式的編譯和連線,該程式會自動呼叫 gcc 實現編譯。
在使用gcc編譯器的時候,我們必須給出一系列必要的呼叫引數和檔名稱。gcc編譯器的呼叫引數大約有100多個,其中多數引數我們可能根本就用不到,這裡只介紹其中最基本、最常用的引數。
gcc最基本的用法是∶gcc [options] [filenames]
其中options就是編譯器所需要的引數,filenames給出相關的檔名稱。
下面我們以例項來說明 gcc 的用法。
假設我們有乙個如下的 c 語言原始檔(hello.c):
#include
int main()
則可以如下呼叫 gcc 命令編譯、連線並生成可執行檔案:
[root@zieckey hello_world]# gcc -o out hello.c
[root@zieckey hello_world]# ./out
hello, world!
[root@zieckey hello_world]#
註解:1. -o output_filename,確定輸出檔案的名稱為output_filename,同時這個名稱不能和原始檔同名。如果不給出這個選項,gcc就給出缺省的可執行檔案a.out。
這裡我們指定生成的輸出檔名為out,當然你也可以改用其他你喜歡的或者更有意義名字。
2. ./out 是執行剛剛通過 gcc 命令生成的源程式的可執行檔案
好的,很簡單對不!?
下面我們來看看c++程式怎麼編譯?
假設我們有乙個如下的 c++ 原始檔(hello.cpp):
#include
using namespace std;
int main()
則可以如下呼叫 g++ 命令編譯、連線並生成可執行檔案:
[root@zieckey hello_world]# g++ -o out hello.cpp
[root@zieckey hello_world]# ./out
hello, world!
[root@zieckey hello_world]#
同樣很簡單,對不?!
是的。確實是很簡單。
好了,下面看看稍稍難點的例項。
清單 calcfactorial.c
// name: calcfactorial.c
// a function is defined in this file.
// author : zieckey
// data : 2006/11/13
#include
int calcfactorial (int n)
else
return calcfactorial (n - 1) * n;
}清單 main.c
// name: main.c
// this file is used to test how to use gcc.it is very ******,ha!
// author : zieckey
// data : 2006/11/13
#include
int calcfactorial (int n);
int main (void)
[root@localhost exam-source-file]# gcc calcfactorial.c main.c
預設情況下生成乙個 a.out 的檔案.執行看看:
[root@localhost exam-source-file]# ./a.out
please input a number which you want to calculator :10
the result of calcfactorial 10 is : 3628800
上面我們簡要介紹了gcc編譯器最常用的功能和用法,更為詳盡的資料可以參看linux系統的聯機幫助(在shell下輸入 「man gcc」 就可以了)。
tcl tk詳解 glob使用例解
新增鏈結描述 名稱glob 返回模式匹配的檔名語法 glob switches?pattern pattern 描述這個命令返回乙個匹配pattern變元的檔案列表,返回的列表並不排序,如果需要排序就需要呼叫lsort命令。如果初始變元使用 開頭,就作為標誌出現,以下所述是目前支援的標誌位 dire...
grep 高階例解
grep 是在linux查詢文字過程最常用的命令,熟悉grep的一些常用命令,可以在個別時候有效提高工作效率。場景一 需要查詢乙個目錄及子目錄所有檔案中出現 aaa 但是同時不能出現bbb的行,查詢不區分大小寫 grep rin aaa grep v bb 解釋 r 遞迴查詢 i 不區分大小寫 n ...
轉 gcc的使用
1。gcc包含的c c 編譯器 gcc,cc,c g gcc和cc是一樣的,c 和g 是一樣的,一般c程式就用gcc編譯,c 程式就用g 編譯 2。gcc的基本用法 gcc test.c這樣將編譯出乙個名為a.out的程式 gcc test.c o test這樣將編譯出乙個名為test的程式,o引數...