CPP(1)
π cpp(1) π
Section 1 Β· GNU π
β¨ NAME β¨
Okay so the whole name is just cpp, which is The C Preprocessor π
π SYNOPSIS π
cpp [-Dmacro[=defn]...] [-Umacro]
[-Idir...] [-iquotedir...]
[-M|-MM] [-MG] [-MF filename]
[-MP] [-MQ target...]
[-MT target...]
infile [[-o] outfile]
Only the most useful options made it into the synopsis, so scroll down for the full list of preprocessor-specific options π«Ά. cpp also takes most gcc driver options, but those aren't listed here, so go read the GCC documentation for the full scoop β.
π DESCRIPTION π
The C preprocessor, often known as cpp, is a macro processor that the C compiler uses automatically to transform your program before compilation β¨. It's called a macro processor because it lets you define macros, which are brief abbreviations for longer constructs π¦.
The C preprocessor is meant to be used only with C, C++, and Objective-C source code π . In the past it got abused as a general text processor π, and it will choke on input that doesn't follow C's lexical rules. For example, apostrophes get read as the start of character constants and cause errors π. You also can't count on it keeping characteristics of the input that don't matter to C-family languages. If a Makefile gets preprocessed, all the hard tabs get removed and the Makefile won't work π.
Having said that, you can lowkey get away with running cpp on things that aren't C πΈ. Other Algol-ish programming languages are often safe (Ada, etc.). Assembly is fine too, with caution. -traditional-cpp mode preserves more white space and is otherwise more permissive. Most of the problems can be avoided by writing C or C++ style comments instead of native language comments, and by keeping macros simple π«Ά.
Wherever possible, use a preprocessor geared to the language you're writing in, periodt π . Modern versions of the GNU assembler have macro facilities. Most high-level programming languages have their own conditional compilation and inclusion mechanism. If all else fails, try a true general text processor, such as GNU M4 π§.
C preprocessors vary a bit in the details π. This manual is about the GNU C preprocessor, which gives you a small superset of the features of ISO Standard C. In its default mode, the GNU C preprocessor skips a few things the standard requires.
Heads up, bestie: these skipped features are rarely, if ever, used, but they can cause surprising changes to the meaning of a program that doesn't expect them π.
To get strict ISO Standard C, use -std=c90, -std=c99, -std=c11 or -std=c17, depending on which version of the standard you want. To get all the mandatory diagnostics, you must also use -pedantic β οΈ.
This manual describes the behavior of the ISO preprocessor π. To minimize gratuitous differences, wherever the ISO preprocessor's behavior doesn't conflict with traditional semantics, the traditional preprocessor should behave the same way π¦. The differences that do exist are detailed in the section Traditional Mode.
For clarity, unless noted otherwise, any references to CPP in this manual mean GNU CPP π.
π OPTIONS π
The cpp command expects two file names as arguments, infile and outfile π. The preprocessor reads infile together with any other files it pulls in with #include, and all the output from the combined input files gets written into outfile β¨.
Either infile or outfile can be -. As infile, that means read from standard input, and as outfile, it means write to standard output πΈ. If either file is left out, it's the same as if - had been given for that file. You can also use the -o outfile option to specify the output file π.
Unless otherwise noted, or the option ends in =, every option that takes an argument can have that argument right after the option, or with a space between the two. So -Ifoo and -I foo have the same effect π¦.
Lots of options have multi-letter names, so you can't group multiple single-letter options together. -dM is very different from -d -M π.
-D nameβ Predefinesnameas a macro with the definition1, no thoughts needed π.-D name=definitionβ The contents ofdefinitionget tokenized and processed as if they showed up during translation phase three in a#definedirective. The important bit: the definition gets truncated by embedded newline characters βοΈ. If you're invoking the preprocessor from a shell (or something shell-like), you might need the shell's quoting syntax to protect characters like spaces that have a meaning in shell syntax π. Want a function-like macro on the command line? Write its argument list with surrounding parentheses before the equals sign (if any). Parentheses mean something to most shells, so quote the option. Withshandcsh,-D'name(args...)=definition'works π§. The-Dand-Uoptions are processed in the order they're given on the command line, and all-imacros fileand-include fileoptions are processed after all the-Dand-Uoptions π.-MMDβ okay so this is basically-MD, but chill π¦ it only mentions your own header files and skips the system ones entirely π-fpreprocessedβ bestie, this tells the preprocessor "hey, this file is already preprocessed, don't redo it" π so macro expansion, trigraph conversion, escaped newline splicing, and most directive processing all get skipped β¨ Comments still get recognized and removed, so you can pass a file made with-Cto the compiler without any drama. In this mode the integrated preprocessor is basically just a tokenizer for the front ends π§
-fpreprocessed is implicit if the input file ends in .i, .ii or .mi. Those are the extensions GCC uses for preprocessed files made by -save-temps πΈ
-fdirectives-onlyβ when preprocessing, handle directives but do not expand macros π· What it does depends on-Eand-fpreprocessed:- With
-E, preprocessing is limited to directives like#define,#ifdef, and#error. Other stuff like macro expansion and trigraph conversion is not performed. The-dDoption is also implicitly enabled π - With
-fpreprocessed, predefinition of command line and most builtin macros is disabled. Contextually dependent macros like__LINE__are handled normally. This lets you compile files that were previously preprocessed with-E -fdirectives-onlyπ«Ά - With both
-Eand-fpreprocessed, the-fpreprocessedrules take precedence. This lets you do full preprocessing of files that were previously preprocessed with-E -fdirectives-onlyπ
- With
-fdollars-in-identifiersβ accept$in identifiers π-fextended-identifiersβ accept universal character names and extended characters in identifiers β¨ This one is on by default for C99 (and later C standards) and C++ π-fno-canonical-system-headersβ when preprocessing, don't shorten system header paths with canonicalization π-fmax-include-depth=depthβ sets the maximum depth of nested#includeπ§Ά The default is200.-ftabstop=widthβ sets the distance between tab stops π This helps the preprocessor report correct column numbers in warnings or errors, even when tabs show up on the line. If the value is less than1or greater than100, the option is ignored. The default is8.-ftrack-macro-expansion[=level]β tracks token locations across macro expansions π This lets the compiler print the current macro expansion stack when an error happens inside a macro. Be warned: using it makes the preprocessor and the compiler eat more memory π The level parameter picks how precise the tracking is, so you can drop the memory usage if you need to:- Level
0turns this option off. - Level
1tracks token locations in a degraded mode for minimal memory overhead. In this mode, every token from expanding an argument of a function-like macro has the same location. - Level
2tracks token locations completely. This is the most memory hungry one π·
- Level
If you give this option no argument, the default level is 2.
Also, -ftrack-macro-expansion=2 is activated by default π
-fmacro-prefix-map=old=newβ when preprocessing files living in directory old, expands__FILE__and__BASE_FILE__as if those files lived in directory new instead πΊοΈ You can use this to turn an absolute path into a relative one by using.for new, which can make builds more reproducible and location independent π± This option also affects__builtin_FILE()during compilation. See also-ffile-prefix-map.-fexec-charset=charsetβ sets the execution character set, used for string and character constants π€ The default isUTF-8. charset can be any encoding the system'siconvlibrary routine supports.-fwide-exec-charset=charsetβ sets the wide execution character set, used for wide string and character constants π The default is one ofUTF-32BE,UTF-32LE,UTF-16BE, orUTF-16LE, whichever matches the width ofwchar_tand the big-endian or little-endian byte order used for code generation. Like-fexec-charset, charset can be any encoding the system'siconvroutine supports.
Heads up bestie β οΈ you'll have problems with encodings that don't fit exactly in wchar_t.
-finput-charset=charsetβ sets the input character set, used for translating from the input file's character set into the source character set GCC uses π₯ If the locale doesn't say, or GCC can't get that info from the locale, the default isUTF-8. The locale or this command-line option can override that, but if they conflict, the command-line option wins πͺ charset can be any encoding the system'siconvroutine supports.-fworking-directoryβ turns on linemarkers in the preprocessor output that tell the compiler the current working directory at preprocessing time π When this is on, the preprocessor emits a second linemarker after the initial one, containing the current working directory followed by two slashes. GCC uses that directory, when it's in the preprocessed input, as the working directory in some debugging information formats π This option is implicitly enabled if debugging information is enabled, but you can stop that with the negated form-fno-working-directory. If-Pis in the command line, this option does nothing, since no#linedirectives get emitted whatsoever π«-A predicate=answerβ makes an assertion with the predicate predicate and the answer answer π§Ύ This form is preferred over the older-A predicate(answer)form, which is still supported, because it doesn't use shell special characters π-A -predicate=answerβ cancels an assertion with the predicate predicate and the answer answer β-Cβ do not discard comments π¨οΈ All comments get passed through to the output file, except comments in processed directives, which get deleted along with the directive.
Be prepared for side effects with-Cβ οΈ It makes the preprocessor treat comments as tokens in their own right. For example, a comment at the start of what would be a directive line turns that line into an ordinary source line, since the first token on the line is no longer a#.
-CCβ do not discard comments, including during macro expansion π¬ This is like-C, except comments inside macros also get passed through to the output file wherever the macro is expanded π
On top of the -C side effects, -CC converts all C++-style comments inside a macro into C-style comments π This stops a later use of that macro from accidentally commenting out the rest of the source line π
ββοΈ
The -CC option is mostly used to support lint comments π§Ή
-Pβ inhibits linemarkers in the preprocessor output π«π This might come in handy when running the preprocessor on something that isn't C code and will be sent to a program that might get confused by the linemarkers π΅-traditionaland-traditional-cppβ try to imitate the behaviour of pre-standard C preprocessors, instead of ISO C preprocessors π¦ GCC doesn't otherwise try to emulate a pre-standard C compiler, and these options are only supported with the-Eswitch, or when invoking CPP explicitly π-trigraphsβ supports ISO C trigraphs πΊ These are three-character sequences that all start with??, which ISO C defines to stand for single 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.
-remapβ enables special code to work around file systems that only allow super short file names, like MS-DOS πΌ-Hβ prints the name of each header file used, along with everything else it normally does π Each name is indented to show how deep it sits in the#includestack. Precompiled header files get printed too, even if they turn out to be invalid. An invalid precompiled header gets printed with...x, and a valid one with...!π₯²-dlettersβ makes debugging dumps during compilation, as specified by letters π΅οΈββοΈ The flags documented here are the ones relevant to the preprocessor. Other letters are either interpreted by the compiler proper, or reserved for future GCC versions, so they're silently ignored π€« If you specify letters whose behaviour conflicts, the result is undefined π-dMβ instead of the normal output, generates a list of#definedirectives for all the macros defined during preprocessing, including predefined macros π This is a great way to find out what's predefined in your version of the preprocessor π Assuming you have no filefoo.h, the command below shows all the predefined macros:
touch foo.h; cpp -dM foo.h
-dDβ like-dM, except it does not include the predefined macros, and it outputs both the#definedirectives and the result of preprocessing π€ Both kinds of output go to the standard output file.-dNβ like-dD, but only emits the macro names, not their expansions π·οΈ-dIβ outputs#includedirectives in addition to the result of preprocessing π-dUβ like-dD, except only macros that are expanded, or whose definedness is tested in preprocessor directives, get output π― The output is delayed until the macro is used or tested, and#undefdirectives are also output for macros that were tested but undefined at that time π§
-fdebug-cppβ this option is only useful for debugging GCC π When used fromCPPor 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.
-I dir,-iquote dir,-isystem dir,-idirafter dirβ okay bestie, these four all do the same core job: they slide dir onto the list of folders CPP digs through for header files during preprocessing πβ¨- If dir begins with
=or$SYSROOT, that=or$SYSROOTgets swapped out for the sysroot prefix. Peep--sysrootand-isysrootfor the deets πΊοΈ -iquoteonly applies to the quote form of the directive,#include "file".-I,-isystem, and-idiraftercover lookups for both#include "file"and#include <file>π- You can stack any number or combo of these on the command line to search a bunch of folders. The lookup order goes like this:
- For the quote form of the include directive, the directory of the current file gets searched first.
- For the quote form, the
-iquotedirs get searched left-to-right, in the order they appear on the command line. - The
-Idirs get scanned left-to-right. - The
-isystemdirs get scanned left-to-right. - The standard system directories get scanned.
- The
-idirafterdirs get scanned left-to-right.
- Want to override a system header with your own version? Use
-I, since those folders get searched before the standard system header directories π¦ But please don't use it to add folders full of vendor-supplied system headers. Use-isystemfor that π« -isystemand-idirafteralso mark the directory as a system directory, so it gets the same VIP treatment as the standard system directories π- If a standard system include directory, or a directory given with
-isystem, is also passed with-I, the-Ioption gets ignored π The directory is still searched, but as a system directory at its normal position in the system include chain. This keeps GCC's procedure for fixing buggy system headers and the ordering for the#include_nextdirective from getting accidentally changed π οΈ If you really need to change the search order for system directories, reach for the-nostdincand/or-isystemoptions instead.
- If dir begins with
-I-β splitting the include path, but this option is deprecated π Use-iquoteinstead for the-Idirs that come before-I-, and then remove the-I-option.- Any dirs given with
-Ibefore-I-only get searched for headers requested with#include "file". They're not searched for#include <file>. If extra dirs are given with-Iafter the-I-, those get searched for all#includedirectives π -I-also stops the directory of the current file from being the first search directory for#include "file". There's no way to override this effect of-I-π«
- Any dirs given with
Heads up, bestie:-I-is deprecated, so swap your pre--I--Idirs over to-iquoteand drop-I-altogether β οΈ
-iprefix prefixβ set prefix as the prefix for any-iwithprefixoptions that come after it π§- If the prefix represents a directory, you should include the final
/π
- If the prefix represents a directory, you should include the final
-iwithprefix dir,-iwithprefixbefore dirβ tack dir onto the prefix you set earlier with-iprefix, then add the resulting directory to the include search path π-iwithprefixbeforedrops it in the same spot-Iwould.-iwithprefixputs it where-idirafterwould π
-isysroot dirβ this is like the--sysrootoption, but it only applies to header files (except on Darwin targets, where it covers both header files and libraries). Check out the--sysrootoption for more info πΈ-imultilib dirβ use dir as a subdirectory of the directory that holds the target-specific C++ headers π§-nostdincβ don't search the standard system directories for header files π Only the dirs explicitly given with-I,-iquote,-isystem, and/or-idirafteroptions (plus the directory of the current file, if appropriate) get searched π-nostdinc++β skip the C++-specific standard directories when hunting for headers, but still search the other standard directories. (This one's used when building the C++ library π οΈ)-Wcomment,-Wcommentsβ warn whenever a/*comment-start sequence shows up inside a/*comment, or whenever a backslash-newline shows up in a//comment. This warning is enabled by-Wallπ₯-Wtrigraphsβ warn if any trigraphs pop up that might change the meaning of the program. Trigraphs inside comments don't get warned about, except the ones that would form escaped newlines π- This option is implied by
-Wall. If-Wallisn't given, this option is still enabled unless trigraphs are enabled π - Want trigraph conversion without the warnings, but still want the other
-Wallwarnings? Use-trigraphs -Wall -Wno-trigraphsπ¦
- This option is implied by
-Wundefβ warn if an undefined identifier gets evaluated in an#ifdirective. Those identifiers get replaced with zero π«-Wexpansion-to-definedβ warn wheneverdefinedshows up in the expansion of a macro (including when the macro gets expanded by an#ifdirective). That usage isn't portable π« This warning is also enabled by-Wpedanticand-Wextraβ¨-Wunused-macrosβ warn about macros defined in the main file that are unused. A macro counts as used if it gets expanded or tested for existence at least once. The preprocessor also warns if the macro hasn't been used at the time it gets redefined or undefined π§Ή- Built-in macros, macros defined on the command line, and macros defined in include files are not warned about π
Note, bestie: 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 that 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
-Wno-endif-labelsβ don't warn whenever an#elseor an#endifis 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 β¨
β¨ ENVIRONMENT β¨
This section is all about the environment variables that change how CPP operates π You can use them to specify directories or prefixes to search when looking 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. These take precedence over environment variables, which in turn take precedence over the configuration of GCC π
CPATH, C_INCLUDE_PATH, CPLUS_INCLUDE_PATH, OBJC_INCLUDE_PATH
Each variable's value is a list of directories separated by a special character, much like PATH, and that's where they go looking for header files π The special character, PATH_SEPARATOR, is target-dependent and gets determined at GCC build time π οΈ For Microsoft Windows-based targets it's a semicolon, and for almost all other targets it's a colon π
CPATH specifies a list of directories to be searched as if they were specified with -I, but after any paths given with -I options on the command line. This environment variable is used regardless of which language is being preprocessed π
The remaining environment variables apply only when preprocessing the particular language they're tied to. Each one specifies a list of directories to be searched as if they were 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 can appear at the beginning or end of a path. For instance, if the value of CPATH is ":/special/include", that has the same effect as -I. -I/special/include π¦
DEPENDENCIES_OUTPUT
If this variable is set, its value specifies how to output dependencies for Make, based on the non-system header files processed by 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, with the target name guessed from the source file name. Or the value can have the form file target, in which case the rules are written to file, 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_DEPENDENCIES
This variable is the same as DEPENDENCIES_OUTPUT (see above), except that system header files are not ignored, so it implies -M rather than -MM. However, the dependence on the main input file is omitted π«
SOURCE_DATE_EPOCH
If this variable is set, its value specifies a UNIX timestamp to be used in place of the current date and time in the __DATE__ and __TIME__ macros, so 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 Jan 1970 00:00:00 represented in ASCII. It's identical to the output of 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 the build process π οΈ
β¨ SEE ALSO β¨
gpl(7), gfdl(7), fsf-funding(7), gcc(1), and the Info entries for cpp and gcc ππ
β¨ COPYRIGHT β¨
Copyright (c) 1987-2021 Free Software Foundation, Inc. π Okay so this is the legal part, bestie, but it's actually giving open-source queen energy π
Permission is granted to copy, distribute, and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation π¦ A copy of the license lives in the man page gfdl(7), so go check it out if you're curious β¨
Also, no cap, this manual has no Invariant Sections πΈ The Front-Cover Texts are (a) (see below), and the Back-Cover Texts are (b) (see below) π«Ά
(a) The FSF's Front-Cover Text is:
A GNU Manual
(b) The FSF's Back-Cover Text is:
You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development.
Ngl the back-cover text is lowkey the sweetest thing ever. Freedom to copy and modify, and your copies help fund GNU development π
That's the whole page, bestie! π It's from gcc-11, dated 2024-07-19, and it's the iconic CPP(1) page π π
π¦ where this page comes from
- ubuntu questing: cpp-11-arm-linux-gnueabihf 11.5.0-7ubuntu1.1cross1
- ubuntu questing: cpp-11-arm-linux-gnueabihf 11.5.0-7ubuntu1cross1
- ubuntu resolute: cpp-11-arm-linux-gnueabihf 11.5.0-11ubuntu2cross1
- ubuntu stonking: cpp-11-arm-linux-gnueabihf 11.5.0-11ubuntu2cross1
- π girlified by claude-haiku-5-5 on : a language model's rewrite of the original, under its terms