C Mastery / Compilers: GCC, Clang, MSVC
Part 5 — Compilation, Linking, and Building

Compilers: GCC, Clang, MSVC

This chapter introduces the three major C compilers — GCC, Clang, and MSVC — their command-line drivers, and their differences.

Why This Matters

You will use at least one of these compilers for every C program. Knowing how to drive each, and where they differ, is essential for portable code and for interpreting diagnostics.

Prerequisites

Core Concept

CompilerProducerNotes
GCCGNUThe classic, ubiquitous on Linux
ClangLLVMFast diagnostics, LLVM-based, clang driver
MSVCMicrosoftWindows, cl.exe, cl driver

All three implement ISO C (with extensions) and follow the same pipeline, but their flags and defaults differ.

Common Invocations

# GCC / Clang
gcc -std=c17 -Wall -Wextra -O2 main.c -o app
clang -std=c17 -Wall -Wextra -O2 main.c -o app

# MSVC (from a developer command prompt)
cl /std:c17 /W4 /O2 main.c

Key Differences

AspectGCCClangMSVC
-Wallmany warningsmany warnings/W4
Language flag-std=c17-std=c17/std:c17
Standard defaultGNU dialectGNU dialectolder C by default
ExtensionsGNU extensionsmany GNU extensionsMS extensions

How It Works

The cc/gcc/clang driver runs the preprocessor, compiler, assembler, and linker. cl.exe does the same under one binary. The compiler front end parses C, the middle end optimizes, and the back end generates target code.

Variations

Clang as a drop-in GCC replacement

Clang's clang driver accepts most GCC flags and even offers a gcc-compatible mode, which is why many build systems treat them interchangeably.

Cross compilers

Both GCC and Clang can be built as cross compilers targeting other architectures (c.build.9).

Common Mistakes

Undefined Behavior

is not UB. Using an extension that the standard reserves can be UB.

Portability

(c.pp.conditional).

Under the Hood

GCC and Clang both parse C into an IR (GCC's GIMPLE/RTL, Clang's LLVM IR), optimize, and emit target code. MSVC uses its own IR. c.compiler.* covers internals.

Practical Usage

Exercises

1. Compile the same program with GCC and Clang and compare warnings. 2. Experiment with -std=c17 vs. -std=gnu17. 3. Look up MSVC's /std and /W options and translate a GCC command line.

Deep Challenge

Write a small program that compiles cleanly under GCC and Clang but triggers a portability warning under -pedantic, and fix it. Explain which extension you were using.

References

Verification

verified.`

Progress

Concept checkboxes

Mastery levels

ConceptCurrent level (0–8)Target level
c.build.gcc05
c.build.clang05
c.build.msvc04