Programming
Compiler Differences
GCC
G++
C++ Programming

What is the difference between g++ and gcc?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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.

text
Source file -> Preprocessor -> Compiler -> Assembler -> Linker -> Executable
                                 ^
                        Same backend for both gcc and g++

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:

Extensiongcc interprets asg++ interprets as
.cCC++
.cpp, .cc, .cxx, .CC++C++
.hC headerC++ header
.iPreprocessed CPreprocessed C++
.iiPreprocessed 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.

bash
1# gcc compiles this as C
2gcc -c utils.c
3
4# g++ compiles the same file as C++
5g++ -c utils.c

You can override the default language with the -x flag:

bash
1# Force gcc to treat a .c file as C++
2gcc -x c++ utils.c -lstdc++
3
4# Force g++ to treat a .cpp file as C
5g++ -x c main.cpp

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.

cpp
1// hello.cpp
2#include <iostream>
3
4int main() {
5    std::cout << "Hello, World!" << std::endl;
6    return 0;
7}
bash
1# g++ links libstdc++ automatically - this works
2g++ -o hello hello.cpp
3
4# gcc does NOT link libstdc++ - this fails with linker errors
5gcc -o hello hello.cpp
6# undefined reference to `std::cout`
7# undefined reference to `std::basic_ostream<...>`
8
9# gcc with explicit library linking - this works
10gcc -o hello hello.cpp -lstdc++

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.).

cpp
1// detect.c
2#include <stdio.h>
3
4int main() {
5    #ifdef __cplusplus
6        printf("Compiled as C++\n");
7    #else
8        printf("Compiled as C\n");
9    #endif
10    return 0;
11}
bash
1gcc -o detect detect.c && ./detect
2# Output: Compiled as C
3
4g++ -o detect detect.c && ./detect
5# Output: Compiled as C++

This distinction matters for libraries that provide dual C/C++ interfaces using extern "C" blocks:

cpp
1// mylib.h
2#ifdef __cplusplus
3extern "C" {
4#endif
5
6void process_data(const char* input);
7
8#ifdef __cplusplus
9}
10#endif

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:

c
1// types.c
2#include <stdlib.h>
3
4int main() {
5    // In C, void* implicitly converts to any pointer type
6    // In C++, this requires an explicit cast
7    int* p = malloc(sizeof(int));
8    *p = 42;
9    free(p);
10    return 0;
11}
bash
1# gcc compiles this as C - no warnings
2gcc -Wall -o types types.c
3
4# g++ compiles this as C++ - error: invalid conversion from 'void*' to 'int*'
5g++ -Wall -o types types.c

Other language differences that surface:

FeatureC (gcc on .c files)C++ (g++ or gcc on .cpp)
void* implicit conversionAllowedRequires explicit cast
// commentsC99+ onlyAlways allowed
bool typeRequires stdbool.h (C99)Built-in
Function overloadingNot supportedSupported
Name manglingNoneApplied to all non-extern-C symbols
Default const linkageExternalInternal

Practical Recommendations

Pure C projects

Use gcc with explicit C standard flags:

bash
gcc -std=c17 -Wall -Wextra -pedantic -o myapp main.c utils.c

Pure C++ projects

Use g++ with explicit C++ standard flags:

bash
g++ -std=c++20 -Wall -Wextra -pedantic -o myapp main.cpp utils.cpp

Mixed C and C++ projects

Compile each language with its respective compiler, then link with g++ to ensure the C++ standard library is included:

bash
1# Compile C files with gcc
2gcc -std=c17 -Wall -c -o utils.o utils.c
3
4# Compile C++ files with g++
5g++ -std=c++20 -Wall -c -o main.o main.cpp
6
7# Link everything with g++ (pulls in libstdc++ automatically)
8g++ -o myapp main.o utils.o

In Makefiles, this pattern is standard:

makefile
1CC       = gcc
2CXX      = g++
3CFLAGS   = -std=c17 -Wall -Wextra
4CXXFLAGS = -std=c++20 -Wall -Wextra
5
6OBJECTS = main.o utils.o
7
8myapp: $(OBJECTS)
9	$(CXX) -o $@ $^
10
11main.o: main.cpp
12	$(CXX) $(CXXFLAGS) -c -o $@ $<
13
14utils.o: utils.c
15	$(CC) $(CFLAGS) -c -o $@ $<

Version and Standard Flags

Both gcc and g++ share the same version number because they are part of the same GCC distribution:

bash
1gcc --version
2# gcc (GCC) 13.2.0
3
4g++ --version
5# g++ (GCC) 13.2.0

The standard flag syntax differs between languages:

bash
1# C standards
2gcc -std=c11 file.c
3gcc -std=c17 file.c
4gcc -std=c23 file.c    # GCC 14+
5
6# C++ standards
7g++ -std=c++17 file.cpp
8g++ -std=c++20 file.cpp
9g++ -std=c++23 file.cpp  # GCC 13+

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

  • gcc and g++ share the same compiler backend. The difference is in front-end defaults: language detection for .c files and automatic libstdc++ linking.
  • Use gcc for C code and g++ for C++ code. This is the simplest rule that avoids both linker errors and language-standard mismatches.
  • g++ treats .c files as C++, which can cause subtle behavioral differences compared to compiling the same files with gcc.
  • 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.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.