Home / How to run the gcc preprocessor

How to run the gcc preprocessor

In some cases it is required to have source code preprocessed by gcc without undergoing the full compilation process. e.g, this might be necessary when embedded SQL is included in C or C++ programs and the preprocesssed file will be passed on to another tool which will convert the SQL in native source code.

To make GCC stop after the preprocessing stage, use the option -E, as explained in GNU - GCC options. In other words, Using the -E parameter with gcc or g++ will produce only the preprocessed source code.

$ gcc -E program.c -o program.preprocessed

The program.preprocessed file will contain the file preprocessed by gcc (Macros will be expanded and all include files will be resolved). This preprocessed output will contain lines such as the following ones.

# 131 "/usr/include/bits/types.h" 3 4
# 1 "/usr/include/bits/typesizes.h" 1 3 4
# 132 "/usr/include/bits/types.h" 2 3 4

These lines are line markers that show from which include files specific source code was taken. If those lines are not desired run the same command adding the -P parameter.

$ gcc -E -P program.c -o program.preprocessed

Leave a Reply