Skip to content

GCC (GNU Compiler Collection)

MarekBykowski edited this page May 12, 2026 · 2 revisions

= GNU Compiler Collection (GCC) =

'''GNU Compiler Collection (GCC)''' is a free and open-source compiler system produced by the GNU Project. Originally called the ''GNU C Compiler'', it was expanded to support multiple programming languages and is now the standard compiler for Linux, embedded systems, and cross-platform development.

== Name ==

'''GNU''' stands for '''GNU's Not Unix''' — a recursive acronym coined by Richard Stallman in 1983 as part of the GNU Project to develop a free Unix-like operating system.

== Supported Languages ==

{| class="wikitable"

! Language !! Frontend !! Command
C
-
C++
-
Fortran
-
Ada
-
Go
}

All frontends share the same optimiser and backend — only the parser and AST differ.

== Compilation Pipeline ==

source.c
   ↓  cpp          (preprocessor — expands #define, #include)
source.i
   ↓  cc1          (compiler — produces assembly)
source.s
   ↓  as           (assembler — produces object file)
source.o
   ↓  ld           (linker — produces final binary)
firmware.elf / a.out

Each stage can be run separately:

gcc -E source.c -o source.i # preprocessor only gcc -S source.c -o source.s # compile to assembly gcc -c source.c -o source.o # compile + assemble, no link gcc source.o -o firmware # link only gcc source.c -o firmware # all in one

== GCC vs MSVC ==

{| class="wikitable" ! || GCC || MSVC |- | Vendor || GNU Project || Microsoft |- | Platforms || Linux, embedded, Windows (MinGW) || Windows |- | Object format || ELF || PE (.exe, .dll) |- | x86-64 calling convention || System V ABI (args: RDI, RSI, RDX...) || Microsoft ABI (args: RCX, RDX, R8...) |- | Used by || Linux kernel, U-Boot, GCC toolchains || Windows kernel, Visual Studio |- | Extensions || attribute, _builtin* || __declspec, __cdecl |}

== Embedded Cross-Compilation ==

For embedded targets, GCC is used as a cross-compiler — runs on the host machine but produces code for a different target architecture.

{| class="wikitable"

! Toolchain !! Target !! Use case
arm-none-eabi-gcc
-
arm-linux-gnueabihf
-
aarch64-linux-gnu-gcc
-
riscv32-unknown-elf-gcc
}

== C Standard Flags ==

CFLAGS += -std=c99 # strict C99 CFLAGS += -std=c11 # strict C11 CFLAGS += -std=gnu11 # C11 + GCC extensions (most common for embedded) CFLAGS += -std=gnu99 # C99 + GCC extensions

Always recommended

CFLAGS += -Wall -Wextra -Wpedantic

== GCC Extensions ==

GCC adds extensions beyond the C standard. Commonly used in Linux kernel and embedded firmware:

=== attribute ===

__attribute__((packed)) // remove struct padding __attribute__((aligned(64))) // force alignment __attribute__((noreturn)) // function never returns __attribute__((section(".ccmram"))) // place in specific memory section __attribute__((weak)) // weak symbol — can be overridden __attribute__((unused)) // suppress unused warning

=== _builtin* ===

__builtin_expect(x, 1) // branch prediction hint (likely) __builtin_expect(x, 0) // branch prediction hint (unlikely) __builtin_popcount(x) // count set bits __builtin_clz(x) // count leading zeros (UB on 0!) __builtin_ctz(x) // count trailing zeros (UB on 0!) __builtin_bswap32(x) // byte swap 32-bit __builtin_bswap64(x) // byte swap 64-bit __builtin_unreachable() // mark dead code path

=== Likely / Unlikely macros (Linux kernel style) ===

#define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0)

if (unlikely(error)) { handle_error(); // compiler moves this out of hot path }

== GCC on Windows ==

GCC does not natively produce Windows PE binaries, but several ports exist:

{| class="wikitable"

! Port !! Description
'''MinGW'''
-
'''MSYS2'''
-
'''Cygwin'''
-
'''WSL'''
}

Most embedded developers on Windows use arm-none-eabi-gcc via MSYS2 or WSL.

== GNU Toolchain ==

GCC is part of the broader GNU toolchain:

{| class="wikitable"

! Tool !! Purpose
gcc
-
ld
-
as
-
objdump
-
objcopy
-
nm
-
size
-
strip
-
gdb
-
glibc
}

== Key Compiler Flags Reference ==

{| class="wikitable"

! Flag !! Meaning
-c
-
-o name
-
-Wall -Wextra
-
-O0
-
-O2
-
-O3
-
-Os
-
-g
-
-I path
-
-L path
-
-l name
-
-D NAME
-
-mcpu=cortex-m4
-
-mthumb
-
-fstack-usage
-
-fno-exceptions
-
-ffreestanding
-
-Map=output.map
}

''Generated during prep session · Swarmer Integra

Clone this wiki locally