Ch1.6: GCC

GCC (GNU Compiler Collection) is one of the oldest and most widely used C and C++ compilers in the world. It is the default compiler on many Linux distributions and is known for its stability, portability, and long history in the open‑source ecosystem.

Understanding GCC’s Toolchain Model

GCC uses a three‑tuple configuration model described in its documentation:

GCC Configure Terms

GCC distinguishes between:

Unlike Clang, GCC cannot freely set --target at compile time. This means GCC must be built separately for every target architecture and sysroot combination. As a result, GCC cross‑compilers are heavier, more rigid, and more difficult to maintain than Clang’s.

Download GCC Toolchains

Prebuilt GCC toolchains are available here:

gcc-releases

Each archive follows the naming pattern:

target.host.tar.xz

For example:

x86_64-linux-gnu.x86_64-w64-mingw32.tar.xz

This means:

After extracting the archive, add the bin directory (containing x86_64-linux-gnu-g++) to your Windows PATH.

Compile a Linux Binary Using GCC on Windows

Once the toolchain is in your PATH, you can compile Linux binaries directly from Windows:

x86_64-linux-gnu-g++ -o hello hello.cpp -O3 -s -flto

Then copy hello to your Linux machine, mark it executable:

chmod +x hello

And run it normally.

Compile Windows Binaries Using GCC

The simplest GCC toolchain to use is:

x86_64-w64-mingw32.x86_64-w64-mingw32.tar.xz

This toolchain runs on Windows and produces Windows binaries. You can use it in two ways:

1. Compile on Windows

Add the toolchain’s bin directory to your Windows PATH, then compile:

g++ -o hello.exe hello.cpp -O3 -std=c++26 -s -flto

You can run hello.exe directly on Windows.

2. Compile on Linux using Wine

On Linux, you can run the Windows GCC compiler through Wine. Set WINEPATH to include the toolchain’s bin directory:

wine g++ -o hello.exe hello.cpp -O3 -std=c++26 -s -flto

This produces a Windows executable, which you can run with:

wine ./hello.exe

This method is often simpler than configuring a native GCC cross‑compiler on Linux.

About Linux‑Target GCC Toolchains

You may also try toolchains such as:

x86_64-linux-gnu.x86_64-linux-gnu.tar.xz

However, Linux binaries built this way may fail to run on your distribution if your system uses an older version of glibc. This is a common issue with GCC‑built Linux binaries.

Clang avoids this problem because it does not depend on the host system’s glibc version when cross‑compiling.

Runtime Libraries

GCC binaries depend on the GNU C++ runtime (libstdc++) and sometimes other GCC runtime libraries. When running a GCC‑built binary, you may need to set:

These environment variables ensure the runtime libraries are found correctly.

Building GCC Yourself

GCC officially encourages users to build GCC from source. They do not provide official prebuilt binaries.

If you attempt to build GCC yourself, you can simplify the process by using:

--disable-bootstrap

This avoids the multi‑stage self‑rebuild process and speeds up compilation significantly.

Alternatively, you can simply use the Windows GCC binaries under Wine, which is often easier and avoids glibc compatibility issues.

Future Chapter: Building Your Own GCC and LLVM Toolchains

In this chapter, we focused on using prebuilt GCC toolchains. However, both GCC and LLVM can also be built entirely from source, including full cross‑compilers.

In a future chapter, we will explain how to create your own GCC and LLVM toolchains using Canadian Compilation — a technique where the build machine, host machine, and target machine are all different. This is the standard method for producing portable and reproducible toolchains for any architecture or operating system.

Once you understand Canadian Compilation, you will be able to build toolchains for Linux, Windows, macOS, Android, WebAssembly, and more, entirely on your own systems.