gcc和g++的区别

参考What is the difference between g++ and gcc?


1.The actual compiler is "cc1" for C and "cc1plus" for C++; both gcc and g++ are drivers (which call the preprocessor/compiler/assembler/linker as needed). 


2.For c++ you should use g++.

It's the same compiler (e.g. the GNU compiler collection). GCC or G++ just choose a different front-end with different default options.

In a nutshell: if you use g++ the frontend will tell the linker that you may want to link with the C++ standard libraries. The gcc frontend won't do that (also it could link with them if you pass the right command line options).


3.You can link the std C++ library in gcc by passing -lstdc++ parameter.


4.GCC: GNU Compiler Collection

  • Referrers to all the different languages that are supported by the GNU compiler.

gcc: GNU C      Compiler
g++: GNU C++ Compiler

The main differences:

  1. gcc will compile: *.c/*.cpp files as C and C++ respectively.
  2. g++ will compile: *.c/*.cpp files but they will all be treated as C++ files.
  3. Also if you use g++ to link the object files it automatically links in the std C++ libraries (gcc does not do this).
  4. gcc compiling C files has less predefined macros.
  5. gcc compiling *.cpp and g++ compiling *.c/*.cpp files has a few extra macros.

Extra Macros when compiling *.cpp files:

#define __GXX_WEAK__ 1
#define __cplusplus 1
#define __DEPRECATED 1
#define __GNUG__ 4
#define __EXCEPTIONS 1
#define __private_extern__ extern


综上所述:

1.gcc和g++只是一个前端,真正执行编译的是c语言对应的"cc1"和c++对应的"cc1plus"。

2.gcc可以编译、链接c和c++,通过后缀名.c和.cpp来区分。但链接时必须添加-lstdc++参数才能保证c++的正确链接,c语言不用。

3.g++用来编译、链接c++,无论后缀是.c或.cpp,都会当做c++来处理。因为是专为c++做的编译工具,所以编译c++一般用g++。

4.gcc和g++在编译c++时会额外添加几个宏,如上第四点提到的。

原文地址:https://www.cnblogs.com/noble/p/4144044.html