What is the difference between g++ and gcc?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Both gcc and g++ are front-end commands in the GNU Compiler Collection, and they invoke the same compiler backend. The practical difference is that g++ treats source files as C++ by default and automatically links the C++ standard library (libstdc++), while gcc treats source files as C by default and does not link the C++ library unless you explicitly ask for it. For pure C code, use gcc. For C++ code, use g++. Mixing them up leads to linker errors or subtle language-standard mismatches.
How GCC and G++ Relate
The GNU Compiler Collection (GCC) is a suite of compilers for C, C++, Objective-C, Fortran, Ada, Go, and other languages. The lowercase gcc command is specifically the C front-end, and g++ is the C++ front-end. Both commands invoke the same internal compilation pipeline (preprocessor, compiler proper, assembler, linker), but they differ in default flags, language assumptions, and library linking behavior.
The front-end determines which language rules apply and which libraries get linked. The compiled code itself goes through the same optimization and code generation stages regardless of which command you use.
Language Detection by File Extension
Both compilers use the file extension to determine the source language:
| Extension | gcc interprets as | g++ interprets as |
.c | C | C++ |
.cpp, .cc, .cxx, .C | C++ | C++ |
.h | C header | C++ header |
.i | Preprocessed C | Preprocessed C++ |
.ii | Preprocessed C++ | Preprocessed C++ |
The key difference is how .c files are handled. When gcc sees a .c file, it compiles it as C. When g++ sees a .c file, it compiles it as C++. This matters because C and C++ have different rules for type checking, name mangling, implicit declarations, and more.
You can override the default language with the -x flag:
Linking Behavior: The Most Important Difference
The most visible difference is what happens at the linking stage. g++ automatically links libstdc++ (the C++ standard library). gcc does not.
The linker errors from gcc are not compilation errors. The source file compiles successfully because gcc recognizes .cpp as C++ and compiles it correctly. The failure happens at the link stage because the C++ standard library symbols are not resolved.
Preprocessor Macro Differences
g++ automatically defines the __cplusplus macro, which many header files use to provide C++-compatible declarations. gcc defines __cplusplus only when compiling files it treats as C++ (.cpp, .cc, etc.).
This distinction matters for libraries that provide dual C/C++ interfaces using extern "C" blocks:
When compiled with gcc, the function uses C linkage (no name mangling). When compiled with g++, the extern "C" block explicitly requests C linkage to prevent C++ name mangling, ensuring the function can be called from C code.
C vs C++ Language Differences That Bite
Because gcc and g++ apply different language rules, the same source file can behave differently:
Other language differences that surface:
| Feature | C (gcc on .c files) | C++ (g++ or gcc on .cpp) |
void* implicit conversion | Allowed | Requires explicit cast |
// comments | C99+ only | Always allowed |
bool type | Requires stdbool.h (C99) | Built-in |
| Function overloading | Not supported | Supported |
| Name mangling | None | Applied to all non-extern-C symbols |
Default const linkage | External | Internal |
Practical Recommendations
Pure C projects
Use gcc with explicit C standard flags:
Pure C++ projects
Use g++ with explicit C++ standard flags:
Mixed C and C++ projects
Compile each language with its respective compiler, then link with g++ to ensure the C++ standard library is included:
In Makefiles, this pattern is standard:
Version and Standard Flags
Both gcc and g++ share the same version number because they are part of the same GCC distribution:
The standard flag syntax differs between languages:
Common Pitfalls
Using gcc to compile and link C++ code without -lstdc++. This produces cryptic linker errors about undefined references to C++ standard library symbols like std::cout or operator new. The compilation succeeds, so the errors seem unrelated to the source code. Use g++ for C++ projects.
Using g++ to compile .c files in a C project. This applies C++ language rules, which are stricter in some areas (like void* conversion) and more permissive in others (like // comments in C89 mode). Code that compiles cleanly under g++ may not be valid C.
Forgetting -lstdc++ must come after object files. The GNU linker resolves symbols left-to-right. Writing gcc -lstdc++ main.o does not work because the library is scanned before the object file that needs it. Write gcc main.o -lstdc++ instead.
Assuming gcc cannot compile C++ at all. gcc can compile .cpp files because it detects the language from the extension. The issue is only at link time, where it does not automatically include libstdc++. This leads to the confusing situation where compilation succeeds but linking fails.
Not specifying an explicit standard flag. The default C and C++ standards vary between GCC versions. GCC 13 defaults to C17 and C++17. Older versions default to C11 and C++14. Always pass -std= explicitly to avoid inconsistent behavior across build environments.
Summary
gccandg++share the same compiler backend. The difference is in front-end defaults: language detection for.cfiles and automaticlibstdc++linking.- Use
gccfor C code andg++for C++ code. This is the simplest rule that avoids both linker errors and language-standard mismatches. g++treats.cfiles as C++, which can cause subtle behavioral differences compared to compiling the same files withgcc.- In mixed-language projects, compile each source file with the appropriate compiler and link with
g++to ensure the C++ standard library is included. - Always specify an explicit language standard (
-std=c17,-std=c++20) to ensure consistent behavior across GCC versions and build environments.

