Linux "cpp" Command Line Options and Examples
The C Preprocessor

The C preprocessor, often known as cpp, is a macro processor that is used automatically by the C compiler to transform your program before compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs. The C preprocessor is intended to be used only with C, C++, and Objective-C source code.


Usage:

cpp [-Dmacro[=defn]...] [-Umacro]
[-Idir...] [-iquotedir...]
[-M|-MM] [-MG] [-MF filename]
[-MP] [-MQ target...]
[-MT target...]
infile [[-o] outfile]






Command Line Options:

-D
Predefine name as a macro, with definition 1.
cpp -D ...
-U
Cancel any previous definition of name, either built in or provided with a -D option.
cpp -U ...
-include
Process file as if "#include "file"" appeared as the first line of the primary source file. However, the first directorysearched for file is the preprocessor's working directory instead of the directory containing the main source file. If not foundthere, 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.
cpp -include ...
-imacros
Exactly like -include, except that any output produced by scanning file is thrown away. Macros it defines remain defined. Thisallows you to acquire all the macros from a header without also processing its declarations.All files specified by -imacros are processed before all files specified by -include.
cpp -imacros ...
-undef
Do not predefine any system-specific or GCC-specific macros. The standard predefined macros remain defined.
cpp -undef ...
-pthread
Define additional macros required for using the POSIX threads library. You should use this option consistently for bothcompilation and linking. This option is supported on GNU/Linux targets, most other Unix derivatives, and also on x86 Cygwin andMinGW targets.
cpp -pthread ...
-MF
When used with -M or -MM, specifies a file to write the dependencies to. If no -MF switch is given the preprocessor sends therules to the same place it would send preprocessed output.When used with the driver options -MD or -MMD, -MF overrides the default dependency output file.
cpp -MF ...
-MT
Change the target of the rule emitted by dependency generation. By default CPP takes the name of the main input file, deletesany directory components and any file suffix such as .c, and appends the platform's usual object suffix. The result is thetarget.An -MT option sets the target to be exactly the string you specify. If you want multiple targets, you can specify them as asingle argument to -MT, or use multiple -MT options.For example, -MT '$(objpfx)foo.o' might give$(objpfx)foo.o: foo.c
cpp -MT ...
-MQ
Same as -MT, but it quotes any characters which are special to Make. -MQ '$(objpfx)foo.o' gives$$(objpfx)foo.o: foo.cThe default target is automatically quoted, as if it were given with -MQ.
cpp -MQ ...
-MD
given. If it is, the driver uses its argument but with a suffix of .d, otherwise it takes the name of the input file, removesany directory components and suffix, and applies a .d suffix.If -MD is used in conjunction with -E, any -o switch is understood to specify the dependency output file, 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.
cpp -MD ...
-MMD
Like -MD except mention only user header files, not system header files.
cpp -MMD ...
-fpreprocessed
Indicate to the preprocessor that the input file has already been preprocessed. This suppresses things like macro expansion,trigraph conversion, escaped newline splicing, and processing of most directives. The preprocessor still recognizes and removescomments, so that you can pass a file preprocessed with -C to the compiler without problems. In this mode the integratedpreprocessor is little more than a tokenizer for the front ends.
cpp -fpreprocessed ...
-fdirectives-only
When preprocessing, handle directives, but do not expand macros.The option's behavior depends on the -E and -fpreprocessed options.With -E, preprocessing is limited to the handling of directives such as "#define", "#ifdef", and "#error". Other preprocessoroperations, such as macro expansion and trigraph conversion are not performed. In addition, the -dD option is implicitlyenabled.With -fpreprocessed, predefinition of command line and most builtin macros is disabled. Macros such as "__LINE__", which arecontextually dependent, are handled normally. This enables compilation of files previously preprocessed with "-E
cpp -fdirectives-only ...
-fdirectives-only".
With both -E and -fpreprocessed, the rules for -fpreprocessed take precedence. This enables full preprocessing of filespreviously preprocessed with "-E -fdirectives-only".
cpp -fdirectives-only". ...
-fdollars-in-identifiers
Accept $ in identifiers.
cpp -fdollars-in-identifiers ...
-fextended-identifiers
Accept universal character names in identifiers. This option is enabled by default for C99 (and later C standard versions) andC++.
cpp -fextended-identifiers ...
-fno-canonical-system-headers
When preprocessing, do not shorten system header paths with canonicalization.
cpp -fno-canonical-system-headers ...
-ftabstop
Set the distance between tab stops. This helps the preprocessor report correct column numbers in warnings or errors, even iftabs appear on the line. If the value is less than 1 or greater than 100, the option is ignored. The default is 8.
cpp -ftabstop ...
-ftrack-macro-expansion[
Track locations of tokens across macro expansions. This allows the compiler to emit diagnostic about the current macro expansionstack when a compilation error occurs in a macro expansion. Using this option makes the preprocessor and the compiler consumemore memory. The level parameter can be used to choose the level of precision of token location tracking thus decreasing thememory consumption if necessary. Value 0 of level de-activates this option. Value 1 tracks tokens locations in a degraded modefor the sake of minimal memory overhead. In this mode all tokens resulting from the expansion of an argument of a function-likemacro have the same location. Value 2 tracks tokens locations completely. This value is the most memory hungry. When this optionis given no argument, the default parameter value is 2.Note that "-ftrack-macro-expansion=2" is activated by default.
cpp -ftrack-macro-expansion[ ...
-fexec-charset
Set the execution character set, used for string and character constants. The default is UTF-8. charset can be any encodingsupported by the system's "iconv" library routine.
cpp -fexec-charset ...
-fwide-exec-charset
Set the wide execution character set, used for wide string and character constants. The default is UTF-32 or UTF-16, whichevercorresponds to the width of "wchar_t". As with -fexec-charset, charset can be any encoding supported by the system's "iconv"library routine; however, you will have problems with encodings that do not fit exactly in "wchar_t".
cpp -fwide-exec-charset ...
-finput-charset
Set the input character set, used for translation from the character set of the input file to the source character set used byGCC. If the locale does not specify, or GCC cannot get this information from the locale, the default is UTF-8. This can beoverridden by either the locale or this command-line option. Currently the command-line option takes precedence if there's aconflict. charset can be any encoding supported by the system's "iconv" library routine.
cpp -finput-charset ...
-fworking-directory
Enable generation of linemarkers in the preprocessor output that let the compiler know the current working directory at the timeof preprocessing. When this option is enabled, the preprocessor emits, after the initial linemarker, a second linemarker withthe current working directory followed by two slashes. GCC uses this directory, when it's present in the preprocessed input, asthe directory emitted as the current working directory in some debugging information formats. This option is implicitly enabledif debugging information is enabled, but this can be inhibited with the negated form -fno-working-directory. If the -P flag ispresent in the command line, this option has no effect, since no "#line" directives are emitted whatsoever.
cpp -fworking-directory ...
-A
Make an assertion with the predicate predicate and answer answer. This form is preferred to the older form -A predicate(answer),which is still supported, because it does not use shell special characters.
cpp -A ...
-traditional-cpp
Try to imitate the behavior of pre-standard C preprocessors, as opposed to ISO C preprocessors.Note that GCC does not otherwise attempt to emulate a pre-standard C compiler, and these options are only supported with the -Eswitch, or when invoking CPP explicitly.
cpp -traditional-cpp ...
-trigraphs
Support ISO C trigraphs. These are three-character sequences, all starting with ??, that are defined by ISO C to stand forsingle characters. For example, ??/ stands for \, so '??/n' is a character constant for a newline.By default, GCC ignores trigraphs, but in standard-conforming modes it converts them. See the -std and -ansi options.
cpp -trigraphs ...
-remap
Enable special code to work around file systems which only permit very short file names, such as MS-DOS.
cpp -remap ...
-dletters
Says to make debugging dumps during compilation as specified by letters. The flags documented here are those relevant to thepreprocessor. Other letters are interpreted by the compiler proper, or reserved for future versions of GCC, and so are silentlyignored. If you specify letters whose behavior conflicts, the result is undefined.
cpp -dletters ...
-dN
but emit only the macro names not their expansions.
cpp -dN ...
-dI
Output #include directives in addition to the result of preprocessing.
cpp -dI ...
-fdebug-cpp
This option is only useful for debugging GCC. When used from CPP or with -E, it dumps debugging information about location maps.Every token in the output is preceded by the dump of the map its location belongs to.When used from GCC without -E, this option has no effect.
cpp -fdebug-cpp ...
-idirafter
Add the directory dir to the list of directories to be searched for header files during preprocessing.If dir begins with =, then the = is replaced by the sysroot prefix; see --sysroot and -isysroot.Directories specified with -iquote apply only to the quote form of the directive, "#include "file"". Directories specified with
cpp -idirafter ...
-I
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:1. For the quote form of the include directive, the directory of the current file is searched first.2. For the quote form of the include directive, the directories specified by -iquote options are searched in left-to-rightorder, as they appear on the command line.3. Directories specified with -I options are scanned in left-to-right order.4. Directories specified with -isystem options are scanned in left-to-right order.5. Standard system directories are scanned.6. Directories specified with -idirafter options are scanned in left-to-right order.You can use -I to override a system header file, substituting your own version, since these directories are searched before thestandard system header file directories. However, you should not use this option to add directories that contain vendor-suppliedsystem 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 treatmentthat is applied to the standard system directories.If a standard system include directory, or a directory specified with -isystem, is also specified with -I, the -I option isignored. The directory is still searched but as a system directory at its normal position in the system include chain. This isto ensure that GCC's procedure to fix buggy system headers and the ordering for the "#include_next" directive are notinadvertently changed. If you really need to change the search order for system directories, use the -nostdinc and/or -isystemoptions.
cpp -I ...
-iprefix
Specify prefix as the prefix for subsequent -iwithprefix options. If the prefix represents a directory, you should include thefinal /.
cpp -iprefix ...
-iwithprefixbefore
Append dir to the prefix specified previously with -iprefix, and add the resulting directory to the include search path.
cpp -iwithprefixbefore ...
-isysroot
This option is like the --sysroot option, but applies only to header files (except for Darwin targets, where it applies to bothheader files and libraries). See the --sysroot option for more information.
cpp -isysroot ...
-imultilib
Use dir as a subdirectory of the directory containing target-specific C++ headers.
cpp -imultilib ...
-nostdinc
Do not search the standard system directories for header files. Only the directories explicitly specified with -I, -iquote,
cpp -nostdinc ...
-isystem
and/or -idirafter options (and the directory of the current file if appropriate) are searched.
cpp -isystem ...
-nostdinc++
Do not search for header files in the C++-specific standard directories, but do still search the other standard directories.(This option is used when building the C++ library.)
cpp -nostdinc++ ...
-Wcomments
Warn whenever a comment-start sequence /* appears in a /* comment, or whenever a backslash-newline appears in a // comment. Thiswarning is enabled by -Wall.
cpp -Wcomments ...
-Wtrigraphs
Warn if any trigraphs are encountered that might change the meaning of the program. Trigraphs within comments are not warnedabout, except those that would form escaped newlines.This option is implied by -Wall. If -Wall is not given, this option is still enabled unless trigraphs are enabled. To gettrigraph conversion without warnings, but get the other -Wall warnings, use -trigraphs -Wall -Wno-trigraphs.
cpp -Wtrigraphs ...
-Wundef
Warn if an undefined identifier is evaluated in an "#if" directive. Such identifiers are replaced with zero.
cpp -Wundef ...
-Wexpansion-to-defined
Warn whenever defined is encountered in the expansion of a macro (including the case where the macro is expanded by an #ifdirective). Such usage is not portable. This warning is also enabled by -Wpedantic and -Wextra.
cpp -Wexpansion-to-defined ...
-Wunused-macros
Warn about macros defined in the main file that are unused. A macro is used if it is expanded or tested for existence at leastonce. The preprocessor also warns if the macro has not been used at the time it is redefined or undefined.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. Toavoid the warning in such a case, you might improve the scope of the macro's definition by, for example, moving it into the firstskipped block. Alternatively, you could provide a dummy use with something like:#if defined the_macro_causing_the_warning#endif
cpp -Wunused-macros ...
-Wno-endif-labels
Do not warn whenever an "#else" or an "#endif" are followed by text. This sometimes happens in older programs with code of theform#if FOO...#else FOO...#endif FOOThe second and third "FOO" should be in comments. This warning is on by default.ENVIRONMENTThis section describes the environment variables that affect how CPP operates. You can use them to specify directories or prefixesto use when searching for include files, or to control dependency output.Note that you can also specify places to search using options such as -I, and control dependency output with options like -M. Thesetake precedence over environment variables, which in turn take precedence over the configuration of GCC.CPATHC_INCLUDE_PATHCPLUS_INCLUDE_PATHOBJC_INCLUDE_PATHEach variable's value is a list of directories separated by a special character, much like PATH, in which to look for headerfiles. The special character, "PATH_SEPARATOR", is target-dependent and determined at GCC build time. For Microsoft Windows-based targets it is a semicolon, and for almost all other targets it is a colon.CPATH specifies a list of directories to be searched as if specified with -I, but after any paths given with -I options on thecommand line. This environment variable is used regardless of which language is being preprocessed.The remaining environment variables apply only when preprocessing the particular language indicated. Each specifies a list ofdirectories to be searched as if specified with -isystem, but after any paths given with -isystem options on the command line.In all these variables, an empty element instructs the compiler to search its current working directory. Empty elements canappear at the beginning or end of a path. For instance, if the value of CPATH is ":/special/include", that has the same effectas -I. -I/special/include.DEPENDENCIES_OUTPUTIf this variable is set, its value specifies how to output dependencies for Make based on the non-system header files processedby the compiler. System header files are ignored in the dependency output.The value of DEPENDENCIES_OUTPUT can be just a file name, in which case the Make rules are written to that file, guessing thetarget name from the source file name. Or the value can have the form file target, in which case the rules are written to filefile using target as the target name.In other words, this environment variable is equivalent to combining the options -MM and -MF, with an optional -MT switch too.SUNPRO_DEPENDENCIESThis variable is the same as DEPENDENCIES_OUTPUT (see above), except that system header files are not ignored, so it implies -Mrather than -MM. However, the dependence on the main input file is omitted.SOURCE_DATE_EPOCHIf this variable is set, its value specifies a UNIX timestamp to be used in replacement of the current date and time in the"__DATE__" and "__TIME__" macros, so that the embedded timestamps become reproducible.The value of SOURCE_DATE_EPOCH must be a UNIX timestamp, defined as the number of seconds (excluding leap seconds) since 01 Jan1970 00:00:00 represented in ASCII; identical to the output of @command{date +%s} on GNU/Linux and other systems that support the%s extension in the "date" command.The value should be a known timestamp such as the last modification time of the source or package and it should be set by thebuild process.
cpp -Wno-endif-labels ...