What is the difference between g++ and gcc?
gcc
and g++
are compiler-drivers of the GNU Compiler Collection (which was once upon a time just the GNU C Compiler).
Even though they automatically determine which backends (cc1
cc1plus
...) to call depending on the file-type, unless overridden with -x language
, they have some differences.
The probably most important difference in their defaults is which libraries they link against automatically.
According to GCC's online documentation link options and how g++ is invoked, g++
is equivalent to gcc -xc++ -lstdc++ -shared-libgcc
(the 1st is a compiler option, the 2nd two are linker options). This can be checked by running both with the -v
option (it displays the backend toolchain commands being run).
GCC: GNU Compiler Collection
- Referrers to all the different languages that are supported by the GNU compiler.
gcc
: GNU C Compilerg++
: GNU C++ Compiler
The main differences:
-
gcc
will compile:*.c\*.cpp
files as C and C++ respectively. -
g++
will compile:*.c\*.cpp
files but they will all be treated as C++ files. - Also if you use
g++
to link the object files it automatically links in the std C++ libraries (gcc
does not do this). -
gcc
compiling C files has fewer predefined macros. -
gcc
compiling*.cpp
andg++
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
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).