Question
What is the difference between gcc and g++, and which one should be used for general C++ development?
Short Answer
By the end of this page, you will understand what gcc and g++ are, how they differ when compiling code, why g++ is usually the right choice for C++ programs, and what happens during compilation and linking.
Concept
gcc and g++ are closely related compiler driver commands from the GNU Compiler Collection.
gccis the traditional command commonly used for compiling C programs.g++is the command commonly used for compiling C++ programs.
They both come from the same toolchain, but they behave differently by default.
The key difference
The biggest practical difference is this:
g++treats source files as C++ and automatically links the C++ standard library.gccis mainly geared toward C, and when used for C++ code, it may not automatically link the libraries a C++ program needs.
That means a C++ source file may compile with gcc in some cases, but linking can fail if your program uses standard C++ features such as:
std::coutstd::stringstd::vector- exceptions
- iostreams
Why this matters
In real programming, compiling is not just turning source code into object files. The final executable must also be linked with the correct libraries.
C++ programs usually depend on the C++ standard library. g++ handles that automatically, which is why it is the normal choice for general C++ development.
General rule
- Use
gccfor C code. - Use
g++for C++ code.
You can force gcc to compile C++ with extra options, but for normal C++ work, g++ is simpler and safer.
Mental Model
Think of gcc and g++ as two front-desk assistants for the same workshop.
gccassumes you are bringing in a C project.g++assumes you are bringing in a C++ project.
Both assistants can send your work to the right machines, but g++ automatically prepares the extra C++ tools and libraries your project usually needs.
So if your project speaks C++, g++ is the assistant that already knows the full checklist.
Syntax and Examples
Basic syntax
Compile a C program:
gcc main.c -o app
Compile a C++ program:
g++ main.cpp -o app
Simple C++ example
#include <iostream>
#include <string>
int main() {
std::string name = "Maya";
std::cout << "Hello, " << name << "\n";
return 0;
}
Compile it with g++:
g++ main.cpp -o app
Run it:
./app
Output:
Hello, Maya
What if you use gcc?
If you try:
Step by Step Execution
Consider this C++ program:
#include <iostream>
int main() {
std::cout << "Hi\n";
return 0;
}
Compile it with:
g++ main.cpp -o app
What happens step by step
-
The compiler driver starts
g++reads the command and sees thatmain.cppis a C++ source file.
-
The source is compiled as C++
- The compiler checks C++ syntax.
- It processes
#include <iostream>. - It translates your C++ code into lower-level object code.
-
The object code is linked
g++links the generated object file with required runtime pieces.- It also links the C++ standard library automatically.
-
The executable is created
- The final program is written to .
Real World Use Cases
When developers use gcc
- Compiling C system utilities
- Building low-level tools
- Working with older C codebases
- Compiling embedded C projects
When developers use g++
- Building C++ command-line tools
- Creating game engine components
- Writing desktop applications in C++
- Compiling competitive programming solutions
- Building libraries that use classes, templates, and STL containers
Practical examples
- A program using
std::vectorandstd::stringshould normally be built withg++. - A pure C file using
printfandmallocshould normally be built withgcc. - A mixed project may use both C and C++ files, but the final link step for a C++ application is often done with
g++.
Real Codebase Usage
In real projects, developers usually do not type raw compile commands for every file by hand. Instead, they use build tools such as:
MakeCMakeNinja- IDE build systems
Common patterns
Separate compile and link steps
g++ -c main.cpp -o main.o
g++ main.o -o app
This is common in larger projects.
Warning flags
Developers often compile with warnings enabled:
g++ -Wall -Wextra -pedantic main.cpp -o app
This helps catch mistakes early.
Choosing a language standard
g++ -std=c++17 main.cpp -o app
Real codebases often require a specific C++ version.
Mixed C and C++ projects
A project may compile some .c files with gcc and some .cpp files with g++, but if the final program is C++, the final link is commonly done with g++ so the C++ runtime and libraries are included correctly.
Common Mistakes
1. Using gcc for a normal C++ program
Broken example:
gcc main.cpp -o app
Possible result:
undefined reference to `std::cout`
Fix
Use g++ instead:
g++ main.cpp -o app
2. Assuming file extension does not matter
A .c file is usually treated as C.
A .cpp file is usually treated as C++.
Broken example:
g++ main.c -o app
This may compile the file as C++ rules, which can change behavior or produce errors if the code was written as C.
Fix
Use the correct file extension and the correct compiler command.
3. Forgetting the C++ standard version
Some code needs a newer standard.
Broken example:
g++ main.cpp -o app
If your code uses modern features, the compiler may reject them.
Fix
Comparisons
| Feature | gcc | g++ |
|---|---|---|
| Main use | C programs | C++ programs |
| Default language expectation | C | C++ |
| Automatically links C++ standard library | Usually no | Yes |
| Best choice for general C++ development | No | Yes |
| Common source files | .c | .cpp, .cc, .cxx |
gcc vs g++ in practice
- If you are writing ordinary C code, is the normal choice.
Cheat Sheet
Quick rule
- C code: use
gcc - C++ code: use
g++
Basic commands
gcc main.c -o app
g++ main.cpp -o app
Useful C++ flags
g++ -std=c++17 -Wall -Wextra -pedantic main.cpp -o app
Compile only
g++ -c main.cpp -o main.o
Link object files
g++ main.o -o app
Important difference
g++automatically links the C++ standard library.gccusually does not do that for you in the same way.
Common file extensions
- C:
.c - C++:
.cpp,.cc,.cxx
Safe beginner habit
If the program uses , , classes, templates, or STL containers, compile with .
FAQ
Should I use gcc or g++ for C++?
Use g++ for general C++ development. It is designed to compile and link C++ programs correctly by default.
Can gcc compile C++ code?
It can in some situations, especially with C++ source files or explicit language options, but it is not the usual choice for building C++ programs.
Why does gcc sometimes fail on C++ programs?
The most common reason is linking. C++ programs often need the C++ standard library, and g++ handles that automatically.
Are gcc and g++ completely different compilers?
No. They are driver commands from the same GNU Compiler Collection. They mainly differ in default behavior.
Why do people recommend g++ for beginners?
Because it reduces confusion. It compiles C++ code as C++ and links the correct standard libraries automatically.
What should I use for mixed C and C++ projects?
You may compile C files with gcc and C++ files with g++, but the final executable for a C++ application is often linked with g++.
Mini Project
Description
Build a tiny C++ console program and compile it correctly with g++. This project demonstrates the practical difference between choosing the right compiler command for a C++ source file and reinforces the idea that C++ programs often depend on the standard library.
Goal
Create and compile a simple C++ greeting program using g++ with a modern C++ standard flag.
Requirements
- Create a C++ source file named
main.cpp - Use
std::stringandstd::coutin the program - Ask the user for their name and print a greeting
- Compile the program with
g++ - Use the
-std=c++17flag when compiling
Keep learning
Related questions
Basic Rules and Idioms for Operator Overloading in C++
Learn the core rules, syntax, and common idioms for operator overloading in C++, including member vs non-member operators.
C++ Base Class Constructor Rules Explained
Learn how C++ base class constructors are called from derived classes, including order, syntax, defaults, and common mistakes.
C++ Casts Explained: C-Style Cast vs static_cast vs dynamic_cast
Learn the difference between C-style casts, static_cast, and dynamic_cast in C++ with clear examples, safety rules, and real usage tips.