The best answers to the question “What is the difference between g++ and gcc?” in the category Dev.
What is the difference between g++ and gcc? Which one of them should be used for general c++ development?
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:
*.c\*.cppfiles as C and C++ respectively.
*.c\*.cppfiles 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 (
gccdoes not do this).
gcccompiling C files has fewer predefined macros.
*.c\*.cppfiles has a few extra macros.
Extra Macros when compiling
#define __GXX_WEAK__ 1 #define __cplusplus 1 #define __DEPRECATED 1 #define __GNUG__ 4 #define __EXCEPTIONS 1 #define __private_extern__ extern
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 (
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).
What is the difference between
gcc has evolved from a single language “GNU C Compiler” to be a multi-language “GNU Compiler Collection”. The term
gcc may still sometimes refer to the “GNU C Compiler” in the context of C programming.
man gcc # GCC(1) GNU # # NAME # gcc - GNU project C and C++ compiler
g++ is the C++ compiler for the GNU Compiler Collection. Like
gnat is the Ada compiler for
gcc. see Using the GNU Compiler Collection (GCC)
For example, the Ubuntu 16.04 and 18.04
man g++ command returns the
GCC(1) manual page.
The Ubuntu 16.04 and 18.04
man gcc states that …
g++accepts mostly the same options as
and that the default …
… use of
gccdoes not add the C++ library.
g++is a program
that calls GCC and automatically specifies linking against the C++
library. It treats .c, .h and .i files as C++ source files instead of
C source files unless -x is used. This program is also useful when
precompiling a C header file with a .h extension for use in C++
gcc man pages for more details on the option variances between
Which one should be used for general c++ development?
g++ can be used for general C++ development with applicable option settings. However, the
g++ default behavior is naturally aligned to a C++ development.
The Ubuntu 18.04 ‘gcc’ man page added, and Ubuntu 20.04 continues to have, the following paragraph:
The usual way to run GCC is to run the executable called
machine-gccwhen cross-compiling, or
machine-gcc-versionto run a specific version of GCC. When you compile C++ programs, you should invoke GCC as
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).