Toolkit: GCC, Clang & MSVC
Three C/C++ compilers cover almost all professional work: GCC (the GNU default on Linux), Clang (LLVM; the default on macOS, Android, FreeBSD and in Xcode, and widely used on Linux and Windows), and MSVC (Microsoft's, in Visual Studio). Build systems hide most of their differences, but you will read their command lines in build logs every day. This lesson is a field guide.
The flags you will see most
GCC and Clang accept almost the same flags. MSVC (cl.exe) has its own syntax; clang-cl is a Clang driver that understands MSVC's.
| Purpose | GCC / Clang | MSVC (cl) |
|---|---|---|
| compile only | -c |
/c |
| output name | -o app |
/Fe:app.exe (exe), /Fo:x.obj (object) |
| preprocess only | -E |
/E or /P |
| assembly output | -S |
/FA |
| include directory | -I dir |
/I dir |
| define a macro | -DNAME=1 |
/DNAME=1 |
| optimise | -O0 -O1 -O2 -O3 -Os |
/Od /O1 (size) /O2 (speed) |
| debug info | -g |
/Zi (separate .pdb file) |
| warnings | -Wall -Wextra (-Werror) |
/W4 (/WX) |
| language standard | -std=c17, -std=c++20 |
/std:c17, /std:c++20 |
| position-independent | -fPIC |
not needed (DLLs are relocated) |
| link a library | -L dir -lname |
name.lib on the command line, /LIBPATH:dir |
| pass to linker | -Wl,option |
/link option |
| dependency file | -MMD -MF x.d |
/showIncludes (parsed by Ninja/MSBuild) |
Same code, different diagnostics
Each compiler has its own warnings and its own analysis, so a second compiler is a cheap extra code review. Here GCC at -O2 -c misses an uninitialised variable that Clang reports, with a suggested fix:
mkdir -p tk && cd tk
cat > bug.c <<'SRC'
#include <limits.h>
#include <stdio.h>
int scale(int x) { return x * 1000; }
int main(int argc, char **argv) {
int n;
if (argc > 5) n = 1;
printf("%d %d\n", scale(INT_MAX / 999 + argc), n);
return 0;
}
SRC
echo "=== gcc"
gcc -Wall -Wextra -O2 -c bug.c
echo "=== clang"
clang -Wall -Wextra -O2 -c bug.c=== gcc
bug.c: In function ‘main’:
bug.c:4:27: warning: unused parameter ‘argv’ [-Wunused-parameter]
4 | int main(int argc, char **argv) {
| ~~~~~~~^~~~
=== clang
bug.c:4:27: warning: unused parameter 'argv' [-Wunused-parameter]
4 | int main(int argc, char **argv) {
| ^
bug.c:6:9: warning: variable 'n' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
6 | if (argc > 5) n = 1;
| ^~~~~~~~
bug.c:7:52: note: uninitialized use occurs here
7 | printf("%d %d\n", scale(INT_MAX / 999 + argc), n);
| ^
bug.c:6:5: note: remove the 'if' if its condition is always true
6 | if (argc > 5) n = 1;
| ^~~~~~~~~~~~~
bug.c:5:10: note: initialize the variable 'n' to silence this warning
5 | int n;
| ^
| = 0
2 warnings generated.
Many projects build with both in CI for exactly this reason. (GCC's -Wmaybe-uninitialized depends on which optimisation passes run, so it catches different cases at different -O levels.)
Sanitizers: checks compiled into the program
Neither compiler warned about the other bug: scale overflows int, which is undefined behaviour in C. Sanitizers add run-time checks to the generated code. They are build flags, so they belong to a separate build configuration (a "sanitizer build" in CI):
mkdir -p tk && cd tk
cat > bug.c <<'SRC'
#include <limits.h>
#include <stdio.h>
int scale(int x) { return x * 1000; }
int main(int argc, char **argv) {
(void)argv;
printf("%d\n", scale(INT_MAX / 999 + argc));
return 0;
}
SRC
gcc -O1 bug.c -o plain && ./plain
gcc -O1 -fsanitize=undefined -fno-sanitize-recover=all bug.c -o checked && ./checked
echo "exit status: $?"-2145333296
bug.c:3:29: runtime error: signed integer overflow: 2149634 * 1000 cannot be represented in type 'int'
exit status: 1
Without the sanitizer the program prints a wrapped-around number and carries on. With -fsanitize=undefined it stops at the exact line. The family: address (out-of-bounds and use-after-free, roughly 2x slower), undefined, thread (data races), memory (Clang only: uninitialised reads). MSVC supports /fsanitize=address.
Which compiler is which?
Build logs show cc and c++, which are just links. Check what they point to, and check versions, because a cache, a lockfile or a reproducibility check is only valid for one exact compiler:
readlink -f "$(command -v cc)" "$(command -v c++)"
gcc -dumpfullversion
clang -dumpversion
echo | gcc -dM -E -x c - | grep -E '^#define __(GNUC|GNUC_MINOR|clang_major)__ '
echo | clang -dM -E -x c - | grep -E '^#define __(GNUC|clang_major)__ '/usr/bin/x86_64-linux-gnu-gcc-13
/usr/bin/x86_64-linux-gnu-g++-13
13.3.0
18.1.3
#define __GNUC__ 13
#define __GNUC_MINOR__ 3
#define __GNUC__ 4
#define __clang_major__ 18
Note the last line: Clang also defines __GNUC__ (claiming GCC 4.2 compatibility), so code that tests #ifdef __GNUC__ to detect GCC matches Clang too. Test __clang__ first.
Other tools in the toolbox
| Tool | Use |
|---|---|
| Compiler Explorer | paste code, see the assembly from dozens of compilers and versions side by side; also runs code (the C, C++ and Rust examples on this site run there) |
compile_commands.json |
one entry per source file with the exact compile command; generated by CMake (CMAKE_EXPORT_COMPILE_COMMANDS=ON), Meson and Ninja (ninja -t compdb), or by bear -- make for other builds. Editors (clangd), linters (clang-tidy) and indexers read it |
clang-tidy, cppcheck |
static analysis beyond compiler warnings |
clang-format |
formatting, usually run as a pre-commit or CI check |
-ftime-trace (Clang), -ftime-report |
where compile time goes; covered in Build Performance & Artifacts |
zig cc |
a Clang-based drop-in C compiler that cross-compiles to many targets with bundled C libraries |
Test yourself
What is the MSVC equivalent of -I include -DDEBUG=1 -c?
-I include -DDEBUG=1 -c/I include /DDEBUG=1 /c/include /debug /compile--include --define
Why does #ifdef __GNUC__ not reliably mean 'compiled by GCC'?
- GCC doesn't define it
- Clang also defines
__GNUC__for compatibility - It is only defined on Windows
- It is a C++ macro
What does -fsanitize=undefined do?
- Removes undefined behaviour at compile time
- Adds run-time checks that report undefined behaviour such as signed overflow when it happens
- Turns warnings into errors
- Makes the program faster