Preprocessing (-E)
The first stage is pure text processing. The preprocessor pastes in #include files, expands macros, and keeps or drops code between #if and #endif. It knows nothing about C types or functions. The language side of this (writing macros, include guards, conditional compilation) is covered in C Preprocessor; here we look at it as a build step: what goes in, what comes out, and why it matters for build tools.
Running only the preprocessor
gcc -E stops after preprocessing and writes the result to standard output. -P drops the # 1 "file" line markers, which keeps the output readable:
mkdir -p pp && cd pp
cat > config.h <<'SRC'
#define APP_NAME "tad-tool"
#define SQUARE(x) ((x) * (x))
SRC
cat > main.c <<'SRC'
#include "config.h"
int area(int side) {
#ifdef DEBUG
log_call("area");
#endif
return SQUARE(side);
}
const char *name = APP_NAME;
SRC
echo "--- default"
gcc -E -P main.c
echo "--- with -DDEBUG"
gcc -E -P -DDEBUG main.c--- default
int area(int side) {
return ((side) * (side));
}
const char *name = "tad-tool";
--- with -DDEBUG
int area(int side) {
log_call("area");
return ((side) * (side));
}
const char *name = "tad-tool";
Two things to notice:
config.his gone: its text was pasted in and its macros were used up. The compiler proper never seesSQUAREorAPP_NAME.-DDEBUGdefines a macro from the command line. The same source gives different code depending on flags, which is why build systems must treat compiler flags as an input: change a-Dand every affected object must be rebuilt.
Headers are big
A single #include <stdio.h> pulls in hundreds of lines from dozens of files. -H prints every header as it is opened, indented by nesting depth (it writes to standard error):
mkdir -p pp && cd pp
printf '#include <stdio.h>\nint main(void) { puts("hi"); return 0; }\n' > hello.c
gcc -E hello.c | wc -l
gcc -H -fsyntax-only hello.c 2>&1 | wc -l
gcc -H -fsyntax-only hello.c 2>&1 | head -5815
39
. /usr/include/stdio.h
.. /usr/include/x86_64-linux-gnu/bits/libc-header-start.h
... /usr/include/features.h
.... /usr/include/features-time64.h
..... /usr/include/x86_64-linux-gnu/bits/wordsize.h
Here a two-line program became 815 lines, read from 39 headers. Each .c file is preprocessed separately, so a header included by 200 source files is read and parsed 200 times. That cost is why projects use precompiled headers and why C++20 modules exist (both are covered in Build Performance & Artifacts). It is also why header dependencies matter: a change to config.h must rebuild every object whose source included it, directly or indirectly. Header Dependencies (-MMD) shows how compilers report that list.
Where headers are searched
#include "file" looks next to the including file first, then in -iquote and -I directories, then in system directories. #include <file> skips the "next to the file" step. -v prints the search list:
mkdir -p pp/include && cd pp
printf '#define VERSION 3\n' > include/version.h
printf '#include <version.h>\nint v = VERSION;\n' > main.c
gcc -E -P main.c 2>&1 | head -3
echo "--- with -Iinclude"
gcc -E -P -Iinclude main.c
gcc -E -v -Iinclude main.c 2>&1 | sed -n '/search starts here/,/End of search/p'main.c:1:10: fatal error: version.h: No such file or directory
1 | #include <version.h>
| ^~~~~~~~~~~
--- with -Iinclude
int v = 3;
#include "..." search starts here:
#include <...> search starts here:
include
/usr/lib/gcc/x86_64-linux-gnu/13/include
/usr/local/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
The first attempt fails because include/ is not on the search path. The -v listing shows the order: your -I directories come before the system ones, so a project header can (accidentally) shadow a system header with the same name.
Predefined macros
The compiler defines macros that describe the target and the compiler itself. Portable code tests them with #if, and they are another reason the same source builds differently on different machines:
gcc -dM -E -x c /dev/null | grep -E '__(x86_64|linux|GNUC|SIZEOF_POINTER|STDC_VERSION)__ ' | sort#define __GNUC__ 13
#define __SIZEOF_POINTER__ 8
#define __STDC_VERSION__ 201710L
#define __linux__ 1
#define __x86_64__ 1
-dM prints the final macro table instead of the source; -x c /dev/null preprocesses an empty C file. __STDC_VERSION__ 201710L shows that GCC 13 compiles C17 (with GNU extensions) by default; pass -std=c11 or -std=c23 to choose another standard explicitly.
Model: a toy preprocessor
The core of the preprocessor is a loop over lines. This model handles #include from an in-memory file table, object-like #define, and #ifdef/#endif:
FILES = {
"main.c": ['#include "config.h"', "#ifdef DEBUG", "trace();", "#endif", "int n = LIMIT;"],
"config.h": ["#define LIMIT 64"],
}
def preprocess(name, macros, out):
skipping = False
for line in FILES[name]:
if line.startswith("#ifdef"):
skipping = line.split()[1] not in macros
elif line.startswith("#endif"):
skipping = False
elif skipping:
continue
elif line.startswith("#include"):
preprocess(line.split('"')[1], macros, out)
elif line.startswith("#define"):
_, key, value = line.split(maxsplit=2)
macros[key] = value
else:
for key, value in macros.items():
line = line.replace(key, value)
out.append(line)
return out
print(preprocess("main.c", {}, []))
print(preprocess("main.c", {"DEBUG": "1"}, []))['int n = 64;']
['trace();', 'int n = 64;']
A real preprocessor works on tokens, not substrings (LIMIT_MAX must not be replaced by 64_MAX), supports function-like macros and nested conditionals, but the shape is the same.
Test yourself
Why must a build system rebuild an object when a -D flag changes, even if no file changed?
- It doesn't need to
- Flags are inputs:
-Dchanges what the preprocessor produces - Because the timestamp changes
- Because the linker requires it
What does gcc -H print?
- Help text
- Each header file opened, indented by include depth
- The assembly
- Only system headers
Which directories are searched first for #include <version.h> with -Iinclude?
- System directories
- The directories given with
-I, then system directories - Only the current directory
- The directory of the compiler