~~NOTOC~~
====== The GCC pre-processor ======
GCC compiler pre-processes source code in a first stage before performing the actual compiling. Pre-processor macros are used to define variables, functions and to conditionally enable/disable code.
This article provides a brief overview about how the GCC pre-compiler works.
===== The GCC -E option =====
The -E option tells GCC to invoke the pre-processor and to display the resulting output to stdout. The GCC man page says the following about this option :
-E Stop after the preprocessing stage; do not run the
compiler proper. The output is in the form of preprocessed
source code, which is sent to the standard output. Input files
which don't require preprocessing are ignored.
===== Example using GCC -E =====
In this example, we will illustrate how to use the GCC pre-processor using the -E option. Consider the following C file :
int main() {
#ifdef FEATURE
This code is enabled when FEATURE is defined
#endif
return 0;
}
If we run the following command :
gcc -E test.c
The result will be :
# 1 "test.c"
# 1 ""
# 1 ""
# 1 "test.c"
int main() {
return 0;
}
However, if we define FEATURE and invoke gcc as follows :
gcc -E -DFEATURE test.c
The resulting output is will be :
# 1 "test.c"
# 1 ""
# 1 ""
# 1 "test.c"
int main() {
This code is enabled when 1 is defined
return 0;
}
The output from GCC -E is the result of interpreting the pre-processor macros (ifdef), in this case, the pre-processor disable and enabled code block according the whether the constant FEATURE is defined.
===== References =====
* [[http://gcc.gnu.org/onlinedocs/cpp/|C Pre-Processor (CPP)]]
* [[http://linux.die.net/man/1/gcc|man gcc]]
* [[http://predef.sourceforge.net/index.php|Pre-defined C/C++ Compiler Macros]]
* [[wp>C preprocessor]]
{{tag>coding unix}}
~~DISCUSSION~~