Most often when you use the C preprocessor you do not have to invoke it explicitly: the C compiler does so automatically. However, the preprocessor is sometimes useful on its own. You can invoke the preprocessor either with the cpp command, or via gcc -E. In GCC, the preprocessor is actually integrated with the compiler rather than a separate program, and both of these commands invoke GCC and tell it to stop after the preprocessing phase.
The cpp options listed here are also accepted by gcc and have the same meaning. Likewise the cpp command accepts all the usual gcc driver options, although those pertaining to compilation phases after preprocessing are ignored.
Only options specific to preprocessing behavior are documented here. Refer to the GCC manual for full documentation of other driver options.
The cpp command expects two file names as arguments, infile and outfile. The preprocessor reads infile together with any other files it specifies with ‘#include’. All the output generated by the combined input files is written in outfile.
Either infile or outfile may be -, which as infile means to read from standard input and as outfile means to write to standard output. If either file is omitted, it means the same as if - had been specified for that file. You can also use the -o outfile option to specify the output file.
Unless otherwise noted, or the option ends in ‘=’, all options which take an argument may have that argument appear either immediately after the option, or with a space between option and argument: -Ifoo and -I foo have the same effect.
Many options have multi-letter names; therefore multiple single-letter options may not be grouped: -dM is very different from ‘-d -M’.
If you are invoking the preprocessor from a shell or shell-like program you may need to use the shell's quoting syntax to protect characters such as spaces that have a meaning in the shell syntax.
If you wish to define a function-like macro on the command line, write its argument list with surrounding parentheses before the equals sign (if any). Parentheses are meaningful to most shells, so you should quote the option. With sh and csh, -D'name(args...)=definition' works.
-D and -U options are processed in the order they
are given on the command line. All -imacros file and
-include file options are processed after all
-D and -U options.
#include "file"appeared as the first line of the primary source file. However, the first directory searched for file is the preprocessor's working directory instead of the directory containing the main source file. If not found there, it is searched for in the remainder of the
#include "..."search chain as normal.
If multiple -include options are given, the files are included
in the order they appear on the command line.
All files specified by -imacros are processed before all files
specified by -include.
Unless specified explicitly (with -MT or -MQ), the object file name consists of the name of the source file with any suffix replaced with object file suffix and with any leading directory parts removed. If there are many included files then the rule is split into several lines using ‘\’-newline. The rule has no commands.
This option does not suppress the preprocessor's debug output, such as -dM. To avoid mixing such debug output with the dependency rules you should explicitly specify the dependency output file with -MF, or use an environment variable like DEPENDENCIES_OUTPUT (see Environment Variables). Debug output is still sent to the regular output stream as normal.
Passing -M to the driver implies -E, and suppresses
warnings with an implicit -w.
This implies that the choice of angle brackets or double quotes in an ‘#include’ directive does not in itself determine whether that header appears in -MM dependency output.
When used with the driver options -MD or -MMD,
-MF overrides the default dependency output file.
#includedirective without prepending any path. -MG also suppresses preprocessed output, as a missing header file renders this useless.
This feature is used in automatic updating of makefiles.
This is typical output:
test.o: test.c test.h test.h:
An -MT option sets the target to be exactly the string you specify. If you want multiple targets, you can specify them as a single argument to -MT, or use multiple -MT options.
For example, -MT '$(objpfx)foo.o' might give
The default target is automatically quoted, as if it were given with
If -MD is used in conjunction with -E, any -o switch is understood to specify the dependency output file (see -MF), but if used without -E, each -o is understood to specify a target object file.
Since -E is not implied, -MD can be used to generate
a dependency output file as a side-effect of the compilation process.
-fpreprocessed is implicit if the input file has one of the
extensions ‘.i’, ‘.ii’ or ‘.mi’. These are the
extensions that GCC uses for preprocessed files created by
The option's behavior depends on the -E and -fpreprocessed options.
With -E, preprocessing is limited to the handling of directives
preprocessor operations, such as macro expansion and trigraph
conversion are not performed. In addition, the -dD option is
With -fpreprocessed, predefinition of command line and most
builtin macros is disabled. Macros such as
__LINE__, which are
contextually dependent, are handled normally. This enables compilation of
files previously preprocessed with
With both -E and -fpreprocessed, the rules for
-fpreprocessed take precedence. This enables full preprocessing of
files previously preprocessed with
-ftrack-macro-expansion=2 is activated by default.
wchar_t. As with -fexec-charset, charset can be any encoding supported by the system's
iconvlibrary routine; however, you will have problems with encodings that do not fit exactly in
#linedirectives are emitted whatsoever.
You should be prepared for side effects when using -C; it
causes the preprocessor to treat comments as tokens in their own right.
For example, comments appearing at the start of what would be a
directive line have the effect of turning that line into an ordinary
source line, since the first token on the line is no longer a ‘#’.
In addition to the side-effects of the -C option, the -CC option causes all C++-style comments inside a macro to be converted to C-style comments. This is to prevent later use of that macro from inadvertently commenting out the remainder of the source line.
The -CC option is generally used to support lint comments.
Note that GCC does not otherwise attempt to emulate a pre-standard
C compiler, and these options are only supported with the -E
switch, or when invoking CPP explicitly.
By default, GCC ignores trigraphs, but in
standard-conforming modes it converts them. See the -std and
touch foo.h; cpp -dM foo.h
shows all the predefined macros.
When used from GCC without -E, this option has no effect.
Directories specified with -iquote apply only to the quote
form of the directive,
Directories specified with -I, -isystem,
or -idirafter apply to lookup for both the
You can specify any number or combination of these options on the command line to search for header files in several directories. The lookup order is as follows:
You can use -I to override a system header file, substituting your own version, since these directories are searched before the standard system header file directories. However, you should not use this option to add directories that contain vendor-supplied system header files; use -isystem for that.
The -isystem and -idirafter options also mark the directory as a system directory, so that it gets the same special treatment that is applied to the standard system directories. See System Headers.
If a standard system include directory, or a directory specified with
-isystem, is also specified with -I, the -I
option is ignored. The directory is still searched but as a
system directory at its normal position in the system include chain.
This is to ensure that GCC's procedure to fix buggy system headers and
the ordering for the
#include_next directive are not inadvertently
If you really need to change the search order for system directories,
use the -nostdinc and/or -isystem options.
See System Headers.
Any directories specified with -I
options before -I- are searched only for headers requested with
"; they are not searched for
>. If additional directories are
specified with -I options after the -I-, those
directories are searched for all ‘#include’ directives.
In addition, -I- inhibits the use of the directory of the current
file directory as the first search directory for
". There is no way to override this effect of -I-.
See Search Path.
This option is implied by -Wall. If -Wall is not
given, this option is still enabled unless trigraphs are enabled. To
get trigraph conversion without warnings, but get the other
-Wall warnings, use ‘-trigraphs -Wall -Wno-trigraphs’.
#ifdirective. Such identifiers are replaced with zero.
Built-in macros, macros defined on the command line, and macros defined in include files are not warned about.
Note: If a macro is actually used, but only used in skipped conditional blocks, then the preprocessor reports it as unused. To avoid the warning in such a case, you might improve the scope of the macro's definition by, for example, moving it into the first skipped block. Alternatively, you could provide a dummy use with something like:
#if defined the_macro_causing_the_warning #endif
#endifare followed by text. This sometimes happens in older programs with code of the form
#if FOO ... #else FOO ... #endif FOO
The second and third
FOO should be in comments.
This warning is on by default.