GCC vs G++: What's the Difference and Which C++ Compiler to Choose?

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: A Drama with Two Names

\n\n

If you've ever tried to compile a C++ program on Linux or macOS, you've likely encountered two similar commands: gcc and g++. At first glance, the difference between them is minimal — both run a compiler, both can work with C and C++. But make the wrong choice, and you get a mountain of linker errors that can ruin an evening even for an experienced developer.

\n\n

This question is one of the most popular on Stack Overflow (over 200,000 views!). Beginners often think: "Since gcc can compile C++, why do I need g++?" And they'd be wrong. Today, we'll finally figure out the catch, using real code examples. You'll learn when to use gcc, when to use g++, and how to avoid the trap of implicit dependencies.

\n\n

A Brief Excursion: What is GCC?

\n\n

GCC (GNU Compiler Collection) is not just one compiler, but a whole set of tools. It includes compilers for C, C++, Fortran, Ada, and other languages. Historically, the acronym stood for "GNU C Compiler," but the project has grown over the years.

\n\n

The gcc command is a driver that, by default, runs the compiler for the C language. The g++ command is the same driver, but with flags that enable C++ support. It seems like a small detail. But it's this "small detail" that determines whether your code will work.

\n\n

The Main Difference: Linking Standard Libraries

\n\n

Imagine you're writing a simple C++ program:

\n\n
#include <iostream>\nint main() {\n    std::cout << "Hello, world!" << std::endl;\n    return 0;\n}\n
\n\n

Let's try to compile it in two ways:

\n\n
gcc main.cpp -o program_gcc\ng++ main.cpp -o program_gpp\n
\n\n

Result:

\n
    \n
  • g++ — compilation will succeed.
  • \n
  • gcc — you will get a linker error undefined reference to `std::cout'.
  • \n
\n\n

Why? Because gcc does not automatically link the libstdc++ library (the C++ standard library). It thinks you're compiling pure C, and C has no iostream, std::string, or other C++ delights. g++ does this automatically.

\n\n

Of course, you can force gcc to work with C++ manually by adding the -lstdc++ flag:

\n\n
gcc main.cpp -lstdc++ -o program_gcc_fixed\n
\n\n

But why reinvent the wheel? g++ does this "out of the box."

\n\n

What About Compiling Pure C?

\n\n

It's symmetric here. If you write a C program and try to compile it with g++, it will likely work. But g++ will treat the code as C++, which means:

\n
    \n
  • Stricter type checking.
  • \n
  • Different rules for implicit type casting.
  • \n
  • Possible false warnings.
  • \n
\n\n

Example. C code:

\n\n
#include <stdio.h>\nint main() {\n    char *s = malloc(100);\n    printf("Hello\\n");\n    free(s);\n    return 0;\n}\n
\n\n

In C, this is fine. In C++, you need an explicit cast (char*)malloc(100). g++ will give an error, while gcc will not. Conclusion: use gcc for C projects, g++ for C++ projects.

\n\n

File Extensions: How Does the Compiler Understand the Language?

\n\n

Another important nuance: gcc and g++ look at the file extension.

\n\n
    \n
  • .c — compiled as C.
  • \n
  • .cpp, .cc, .cxx — compiled as C++.
  • \n
\n\n

If you give gcc a file with the .cpp extension, it will understand it's C++ and use the correct frontend (language compiler). But the linking will still be without

Blogs

Book Recommendations