66 lines
7.1 KiB
HTML
66 lines
7.1 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh" dir="ltr" class="client-nojs" xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh">
|
|
<head><meta http-equiv="x-ua-compatible" content="ie=edge">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<title>预处理器</title>
|
|
<link rel="stylesheet" href="ext.css" />
|
|
<link rel="stylesheet" href="site_modules.css" />
|
|
</head>
|
|
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_preprocessor skin-cppreference2 action-view cpp-navbar">
|
|
<div id="cpp-content-base">
|
|
<div id="content"><a id="top"></a>
|
|
<h1 id="firstHeading" class="firstHeading">预处理器</h1>
|
|
<div id="bodyContent">
|
|
<div id="contentSub"><span class="subpages">< <a href="cpp.html">cpp</a></span></div>
|
|
<div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr" xml:lang="zh">
|
|
<p>预处理器在编译前的<a href="cpp-language-translation_phases.html">翻译阶段 4</a> 执行。预处理的结果是单个文件,接下来它会被传递给实际编译器。</p>
|
|
<h3><span class="mw-headline" id=".E6.8C.87.E4.BB.A4">指令</span></h3>
|
|
<p>预处理指令控制预处理器的行为。每个指令占据一行并拥有下列格式:</p>
|
|
<ul>
|
|
<li><code>#</code> 字符</li>
|
|
<li>预处理指令(<code>define</code>、<code>undef</code>、<code>include</code>、<code>if</code>、<code>ifdef</code>、<code>ifndef</code>、<code>else</code>、<code>elif</code><span class="t-rev-inl t-since-cxx23"><span>、<code>elifdef</code>、<code>elifndef</code></span> <span><span class="t-mark-rev t-since-cxx23">(C++23 起)</span></span></span>、<code>endif</code>、<code>line</code>、<code>error</code>、<code>pragma</code> 之一)<sup id="cite_ref-1" class="reference"><a href="cpp-preprocessor.html#cite_note-1">[1]</a></sup></li>
|
|
<li>实参(取决于指令)</li>
|
|
<li>换行符</li>
|
|
</ul>
|
|
<p>允许空指令(即在 <code>#</code> 后直接换行),而它没有效果。</p>
|
|
<table class="t-rev-begin">
|
|
<tr class="t-rev t-since-cxx20">
|
|
<td>
|
|
<p><a href="cpp-language-modules.html"><code>module</code> 与 <code>import</code> 指令</a>同样是预处理指令。</p>
|
|
</td>
|
|
<td><span class="t-mark-rev t-since-cxx20">(C++20 起)</span></td>
|
|
</tr>
|
|
</table>
|
|
<p>预处理指令不得来自宏展开。</p>
|
|
<div dir="ltr" class="mw-geshi" style="text-align: left;">
|
|
<div class="cpp source-cpp">
|
|
<pre class="de1"><span class="co2">#define EMPTY</span>
|
|
EMPTY <span class="co2"># include <file.h> // 不是预处理指令</span></pre></div>
|
|
</div>
|
|
<h3><span class="mw-headline" id=".E8.83.BD.E5.8A.9B">能力</span></h3>
|
|
<p>预处理器有能力翻译源文件:</p>
|
|
<ul>
|
|
<li><b><a href="cpp-preprocessor-conditional.html">有条件</a></b>编译源文件的某些部分(由 <code>#if</code>、<code>#ifdef</code>、<code>#ifndef</code>、<code>#else</code>、<code>#elif</code><span class="t-rev-inl t-since-cxx23"><span>、<code>elifdef</code>、<code>elifndef</code></span> <span><span class="t-mark-rev t-since-cxx23">(C++23 起)</span></span></span> 和 <code>#endif</code> 指令控制)。</li>
|
|
<li><b><a href="cpp-preprocessor-replace.html">替换</a></b>文本宏,同时可能对标识符进行拼接或加引号(由 <code>#define</code> 和 <code>#undef</code> 指令与 <code>#</code> 和 <code>##</code> 运算符控制)。</li>
|
|
<li><b><a href="cpp-preprocessor-include.html">包含</a></b>其他文件(由 <code>#include</code> 指令控制<span class="t-rev-inl t-since-cxx17"><span>并以 <code>__has_include</code> 检查</span> <span><span class="t-mark-rev t-since-cxx17">(C++17 起)</span></span></span>)。</li>
|
|
<li>引发<b><a href="cpp-preprocessor-error.html">错误</a></b>(由 <code>#error</code> 指令控制)</li>
|
|
</ul>
|
|
<p>能控制预处理器的下列方面:</p>
|
|
<ul>
|
|
<li><b><a href="cpp-preprocessor-impl.html">由实现定义的</a></b>行为(由 <code>#pragma</code> 指令<span class="t-rev-inl t-since-cxx11"><span>和 <code>_Pragma</code> 运算符</span> <span><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></span></span>控制)。</li>
|
|
<li>对预处理器可用的<b><a href="cpp-preprocessor-line.html">文件名和行信息</a></b>(由 <code>#line</code> 指令控制)</li>
|
|
</ul>
|
|
<h3><span class="mw-headline" id=".E8.84.9A.E6.B3.A8">脚注</span></h3>
|
|
<ol class="references">
|
|
<li id="cite_note-1"><span class="mw-cite-backlink"><a href="cpp-preprocessor.html#cite_ref-1">↑</a></span> <span class="reference-text">这些指令是标准定义的。标准不定义其他指令的行为:它们可以被忽略、拥有一些有用的含义或导致编译时错误。即使忽略,也会在预处理器完成工作时将它们从源码中移除。一种常用的非标准扩展是 <code>#warning</code> 指令,它在编译期间放出一条用户定义的消息。</span></li>
|
|
</ol>
|
|
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
|
|
<table class="t-dsc-begin"></table>
|
|
</div>
|
|
<div class="visualClear"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|