|
| 1 | +--- |
| 2 | +title: preprocessor |
| 3 | +cppdoc: |
| 4 | + keys: ["cpp.lang.main"] |
| 5 | +--- |
| 6 | + |
| 7 | +import { Desc, DescList, DocLink } from '@components/index'; |
| 8 | +import { Revision, RevisionBlock } from "@components/revision"; |
| 9 | +import { DR, DRList } from "@components/defect-report"; |
| 10 | + |
| 11 | +The preprocessor is executed at <DocLink src="/cpp/language/translation_phases" anchor="Phase_4">translation phase 4</DocLink>, before the compilation. The result of preprocessing is a single file which is then passed to the actual compiler. |
| 12 | + |
| 13 | +## Directives |
| 14 | + |
| 15 | +The preprocessing directives control the behavior of the preprocessor. Each directive occupies one line and has the following format: |
| 16 | + |
| 17 | +* the `#` character. |
| 18 | +* a sequence of: |
| 19 | + * a standard-defined directive name (listed [below](#Capabilities)) followed by the corresponding arguments, or |
| 20 | + * one or more <DocLink src="/cpp/language/translation_phases" anchor="Phase_3">preprocessing tokens</DocLink> where the beginning token is not a standard-defined directive name, in this case the directive is conditionally-supported with implementation-defined semantics <Revision until="C++23">(e.g. a common non-standard extension is the directive `#warning` which emits a user-defined message during compilation)</Revision>, or |
| 21 | + * nothing, in this case the directive has no effect. |
| 22 | +* a line break. |
| 23 | + |
| 24 | +<Revision since="C++20">The <DocLink src="/cpp/language/modules">module and import directives</DocLink> are also preprocessing directives.</Revision> |
| 25 | + |
| 26 | +Preprocessing directives must not come from macro expansion. |
| 27 | + |
| 28 | +```cpp |
| 29 | +#define EMPTY |
| 30 | +EMPTY # include <file.h> // not a preprocessing directive |
| 31 | +``` |
| 32 | + |
| 33 | +## Capabilities |
| 34 | + |
| 35 | +The preprocessor has the source file translation capabilities: |
| 36 | + |
| 37 | +* **<DocLink src="/cpp/preprocessor/conditional">conditionally</DocLink>** compile parts of source file (controlled by directive `#if`, `#ifdef`, `#ifndef`, `#else`, <Revision since="C++23">`#elif`, `#elifdef`, `#elifndef`</Revision>, and `#endif`). |
| 38 | +* **<DocLink src="/cpp/preprocessor/replace">replace</DocLink>** text macros while possibly concatenating or quoting identifiers (controlled by directives `#define` and `#undef`, and operators `#` and `##`). |
| 39 | +* **<DocLink src="/cpp/preprocessor/include">include</DocLink>** other files (controlled by directive `#include` <Revision since="C++23">and checked with `__has_include` </Revision>). |
| 40 | +* cause an **<DocLink src="/cpp/preprocessor/error">error</DocLink>** <Revision since="C++17">or **<DocLink src="/cpp/preprocessor/error">warning</DocLink>** </Revision> (controlled by directive `#error` <Revision since="C++23">or `#warning` respectively</Revision>). |
| 41 | + |
| 42 | +The following aspects of the preprocessor can be controlled: |
| 43 | + |
| 44 | +* **<DocLink src="/cpp/preprocessor/impl">implementation-defined</DocLink>** behavior (controlled by directive `#pragma` <Revision since="C++11">and operator `_Pragma` </Revision>). In addition, some compilers support (to varying degrees) the operator `__pragma` as a *non-standard* extension. |
| 45 | +* **<DocLink src="/cpp/preprocessor/line">file name and line information</DocLink>** available to the preprocessor (controlled by directive `#line`). |
| 46 | + |
| 47 | +## Defect reports |
| 48 | + |
| 49 | +The following behavior-changing defect reports were applied retroactively to previously published C++ standards. |
| 50 | + |
| 51 | +<DRList> |
| 52 | + <DR kind="cwg" id={2001} std="C++98"> |
| 53 | + <Fragment slot="behavior-published"> |
| 54 | + the behavior of using non-standard-defined directives was not clear |
| 55 | + </Fragment> |
| 56 | + <Fragment slot="correct-behavior"> |
| 57 | + made conditionally-supported |
| 58 | + </Fragment> |
| 59 | + </DR> |
| 60 | +</DRList> |
| 61 | + |
| 62 | +## See also |
| 63 | + |
| 64 | +<DescList> |
| 65 | +<Desc> |
| 66 | +<DocLink slot="item" src="/cpp/preprocessor/replace" anchor="Predefined_macros">C++ documentation</DocLink> for <span>Predefined Macro Symbols</span> |
| 67 | +</Desc> |
| 68 | +<Desc> |
| 69 | +<DocLink slot="item" src="/cpp/symbol_index/macro">C++ documentation</DocLink> for <span>Macro Symbol Index</span> |
| 70 | +</Desc> |
| 71 | +<Desc> |
| 72 | +<DocLink slot="item" src="/c/preprocessor">C documentation</DocLink> for <span>preprocessor</span> |
| 73 | +</Desc> |
| 74 | +</DescList> |
0 commit comments