HOWTO · C++

How to Compile a C++ Program Using GCC

This article discusses how to compile a C++ program using the GCC compiler. Learn the essential commands, installation steps, and methods for debugging and linking multiple files. Perfect for beginners and experienced programmers looking to enhance their C++ skills.

On this page

To compile and run a C++ source file with GCC, use the g++ compiler driver: run g++ -o hello hello.cpp, then run ./hello on Linux or macOS. GCC is the GNU Compiler Collection; g++ selects the C++ build behavior, including linking the C++ standard library for an ordinary program. The steps below cover compiler availability, a first program, common diagnostics, and a small multi-file build.

Understanding GCC and Its Importance

GCC is a compiler collection that includes drivers for several languages. For C++ work, g++ is usually the simplest driver to choose. A build translates source code and links the resulting object code and required libraries into an executable. A one-command build can perform both stages, while a larger project may compile files separately before linking them.

The gcc driver can compile C++ source, but it does not automatically link the C++ standard library in the same way g++ does. On GNU/Linux toolchains, gcc can be used with -lstdc++ to request that library explicitly; library order and platform details can matter, so use g++ for routine C++ builds. Source files commonly use .cpp or .cc; the uppercase .C extension is also treated as C++ by GCC on case-sensitive systems.

Installing GCC

First check whether a C++ compiler is already available by running g++ --version in a terminal. The command prints the installed compiler’s version when found. If the command is unavailable, install a C++ toolchain using your operating system’s package manager or the toolchain provider recommended for that system. Package names and installation steps vary; on Ubuntu and some related distributions, build-essential includes GCC and other build tools. On Windows, install a GCC toolchain such as MinGW-w64 and make its bin directory available on PATH, or use another supported C++ toolchain. Follow the current instructions for your distribution or toolchain rather than relying on a version number from an older example.

Writing Your First C++ Program

Create a file named hello.cpp in a working directory. This program writes one line to standard output:

Run this example:

#include <iostream>

int main() {
    std::cout << "Hello, World!\n";
    return 0;
}
Hello, World!

#include <iostream> provides the stream declarations used by std::cout. The main function is the program’s entry point, and \n ends the printed line.

Compiling the C++ Program

From the directory containing hello.cpp, compile and name the executable with g++ -o hello hello.cpp. The -o option supplies the output name; without it, GCC uses a default name such as a.out on many Unix-like systems. The result is an executable, not an object file. Run it with ./hello on Linux or macOS. In Windows command prompts, the corresponding invocation is commonly hello.exe from the current directory; exact invocation depends on the shell and toolchain.

For a GNU/Linux-oriented alternative, gcc -o hello hello.cpp -lstdc++ explicitly requests the C++ standard library. This can be useful when a build system requires the gcc driver, but g++ is preferable for a normal C++ program because it selects the appropriate C++ linking behavior automatically.

Debugging Compilation Errors

Read a diagnostic from left to right: it identifies a source file and line, then describes the problem. For example, if a program uses cout without including <iostream> or qualifying it as std::cout, a diagnostic may report that cout was not declared. Add the missing include and use std::cout, then rebuild. Exact wording and line numbers vary by compiler version and source layout.

You can ask GCC to enable common warnings with g++ -Wall -o hello hello.cpp. -Wall is a useful starting point, but it does not enable every warning GCC provides. Warnings are diagnostic messages that can point to suspicious code; review them and decide whether the code or the build options should change.

Linking Multiple Files

A program can define a function in one source file and call it from another. For example, main.cpp can include <iostream>, declare int add(int, int);, and define int main() { std::cout << add(2, 3) << '\n'; }. In functions.cpp, define int add(int a, int b) { return a + b; }. These matching declaration and definition let the compiler check the call and the linker connect it to the implementation.

Build both source files in one command: g++ -o my_program main.cpp functions.cpp. This compiles each source file and links them into the executable my_program. Run ./my_program on Linux or macOS; the example prints 5. On Windows, run the resulting my_program.exe using the invocation supported by your shell.

Conclusion

For an ordinary GCC C++ build, use g++ -o program source.cpp, then run the executable produced by that command. The gcc driver can compile C++ too, but GNU/Linux builds may need -lstdc++ added explicitly. Choosing g++ avoids that extra library option in the usual case.

FAQ

  1. What is GCC? GCC is the GNU Compiler Collection, which provides compiler drivers for C++, C, and other languages.
  2. How can I check whether GCC is installed? Run g++ --version to check for the C++ driver, or gcc --version to check the C driver.
  3. Why use g++ instead of gcc for C++? g++ automatically selects C++ linking behavior, including the standard library needed by typical programs. With gcc, a GNU/Linux build may need -lstdc++ explicitly.
  4. Can one command compile several C++ files? Yes. List each source file in the g++ command, such as g++ -o my_program main.cpp functions.cpp.
  5. What does -o do? It names the output file, which is an executable for a complete build command such as g++ -o hello hello.cpp.