GCC:GNU Compiler Collection 是一個編譯器集合,它可以編譯C、C++、JAV、Fortran、Pascal、Object-C、Ada等語言。
gcc是GCC中的GUN C Compiler(C 編譯器)
g++是GCC中的GUN C++ Compiler(C++編譯器)
一個有趣的事實就是,就本質而言,gcc和g++並不是編譯器,也不是編譯器的集合,它們只是一種驅動器,根據參數中要編譯的文件的類型,調用對應的GUN編譯器而已,比如,用gcc編譯一個c文件的話,會有以下幾個步驟:
Step1:Call a preprocessor, like cpp.
Step2:Call an actual compiler, like cc or cc1.
Step3:Call an assembler, like as.
Step4:Call a linker, like ld
由於編譯器是可以更換的,所以gcc不僅僅可以編譯C文件
所以,更准確的說法是:gcc調用了C compiler,而g++調用了C++ compiler
gcc和g++的主要區別
1. 對於 *.c和*.cpp文件,gcc分別當做c和cpp文件編譯(c和cpp的語法強度是不一樣的)
2. 對於 *.c和*.cpp文件,g++則統一當做cpp文件編譯
3. 使用g++編譯文件時,g++會自動鏈接標准庫STL,而gcc不會自動鏈接STL
4. gcc在編譯C文件時,可使用的預定義宏是比較少的
5. gcc在編譯cpp文件時/g++在編譯c文件和cpp文件時(這時候gcc和g++調用的都是cpp文件的編譯器),會加入一些額外的宏,這些宏如下:
#define __GXX_WEAK__ 1
#define __cplusplus 1
#define __DEPRECATED 1
#define __GNUG__ 4
#define __EXCEPTIONS 1
#define __private_extern__ extern
6. 在用gcc編譯c++文件時,為了能夠使用STL,需要加參數 –lstdc++ ,但這並不代表 gcc –lstdc++ 和 g++等價,它們的區別不僅僅是這個
主要參數
-g - turn on debugging (so GDB gives morefriendly output)
-Wall - turns on most warnings
-O or -O2 - turn on optimizations
-o - name of the output file
-c - output an object file (.o)
-I - specify an includedirectory
-L - specify a libdirectory
-l - link with librarylib.a
使用示例:g++ -ohelloworld -I/homes/me/randomplace/include helloworld.C