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
c.build.1— the compilation pipeline.
Core Concept
| Compiler | Producer | Notes |
|---|---|---|
| GCC | GNU | The classic, ubiquitous on Linux |
| Clang | LLVM | Fast diagnostics, LLVM-based, clang driver |
| MSVC | Microsoft | Windows, 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
| Aspect | GCC | Clang | MSVC |
|---|---|---|---|
-Wall | many warnings | many warnings | /W4 |
| Language flag | -std=c17 | -std=c17 | /std:c17 |
| Standard default | GNU dialect | GNU dialect | older C by default |
| Extensions | GNU extensions | many GNU extensions | MS 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
- Assuming one compiler's extensions work on another.
- Using
-std=c17but relying on GNU extensions (use-std=gnu17for that). - Ignoring warnings (
-Wall -Wextrashould be a default habit).
Undefined Behavior
- Compiler extensions can change the meaning of code, but the compiler itself
is not UB. Using an extension that the standard reserves can be UB.
Portability
- Write to ISO C for portability; use extensions only behind feature detection
(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
- Compile with
-std=c17 -Wall -Wextra(or/std:c17 /W4) as a baseline. - Use
-pedanticfor strict conformance warnings. - Test with at least two compilers for portability.
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.
Related Concepts
c.build.8— compiler flags.c.build.9— cross compilation.c.compiler.4— compiler internals.
References
- GCC manual, Clang documentation, Microsoft C/C++ documentation.
Verification
- Compiler flag differences are documented behavior.
VERIFIED - No example was executed during generation unless noted. `Execution not
verified.`
Progress
- [ ] Read
- [ ] Understand
- [ ] Complete examples
- [ ] Complete exercises
- [ ] Complete deep challenge
Concept checkboxes
- [ ] GCC
- [ ] Clang
- [ ] MSVC
- [ ] Compiler differences
Mastery levels
| Concept | Current level (0–8) | Target level |
|---|---|---|
| c.build.gcc | 0 | 5 |
| c.build.clang | 0 | 5 |
| c.build.msvc | 0 | 4 |