Question

Difference Between gcc and g++ for C++ Development

cppgccg++

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.

  • gcc is 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.
  • gcc is 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::cout
  • std::string
  • std::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 gcc for 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.

  • gcc assumes 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.

Take Quiz

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:

gcc main.cpp -o app

gcc may compile the C++ source, but the link step can fail because the C++ standard library is not automatically linked in the same way g++ does.

That is why g++ is the standard command for beginner and professional C++ development.

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

  1. The compiler driver starts

    • g++ reads the command and sees that main.cpp is a C++ source file.
  2. 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.
  3. The object code is linked

    • g++ links the generated object file with required runtime pieces.
    • It also links the C++ standard library automatically.
  4. The executable is created

    • The final program is written to app.
  5. You run the program

    • ./app prints Hi.

Why linking matters here

std::cout comes from the C++ standard library. If that library is not linked properly, the final executable cannot be built, even if the source code itself was valid.

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::vector and std::string should normally be built with g++.
  • A pure C file using printf and malloc should normally be built with gcc.
  • A mixed project may use both C and C++ files, but the final link step for a C++ application is often done with g++.
Take Quiz

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:

  • Make
  • CMake
  • Ninja
  • 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.

Build configuration

In tools like CMake, you usually specify the language and compiler settings once, and the tool uses the right compiler commands behind the scenes.

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

Specify the standard explicitly:

g++ -std=c++17 main.cpp -o app

4. Thinking gcc and g++ are completely different compilers

They are part of the same GNU toolchain. The difference is mostly in default language handling and linking behavior.

Fix

Remember:

  • same compiler collection
  • different default role
  • g++ is the usual driver for C++ builds

5. Linking object files with the wrong driver

If object files contain C++ code and you link with gcc, you may miss the C++ standard library.

Fix

Use g++ for the final link step of C++ programs.

Comparisons

Featuregccg++
Main useC programsC++ programs
Default language expectationCC++
Automatically links C++ standard libraryUsually noYes
Best choice for general C++ developmentNoYes
Common source files.c.cpp, .cc, .cxx

gcc vs g++ in practice

  • If you are writing ordinary C code, gcc is the normal choice.
  • If you are writing ordinary C++ code, g++ is the normal choice.
  • For a C++ executable, g++ is typically the safer final command because it links the right runtime support.

Compared with using build tools

ApproachBest for
Direct g++ commandSmall programs, learning, quick testing
Makefile/CMakeMedium to large projects
IDE build systemProjects managed through an editor or IDE
Take Quiz

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.
  • gcc usually 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 std::cout, std::string, classes, templates, or STL containers, compile with g++.

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

Does g++ support modern C++ standards?

Yes, usually with a flag such as -std=c++17, -std=c++20, or another supported version.

Take Quiz

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::string and std::cout in the program
  • Ask the user for their name and print a greeting
  • Compile the program with g++
  • Use the -std=c++17 flag when compiling
Take Quiz

Keep learning

Related questions

View more