⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 computed-includes.html

📁 gcc手册
💻 HTML
字号:
<html lang="en">

<head>

<title>The C Preprocessor</title>

<meta http-equiv="Content-Type" content="text/html">

<meta name="description" content="The C Preprocessor">

<meta name="generator" content="makeinfo 4.3">

<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">

<!--

Copyright &copy; 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,

1997, 1998, 1999, 2000, 2001, 2002, 2003

Free Software Foundation, Inc.



   <p>Permission is granted to copy, distribute and/or modify this document

under the terms of the GNU Free Documentation License, Version 1.1 or

any later version published by the Free Software Foundation.  A copy of

the license is included in the

section entitled "GNU Free Documentation License".



   <p>This manual contains no Invariant Sections.  The Front-Cover Texts are

(a) (see below), and the Back-Cover Texts are (b) (see below).



   <p>(a) The FSF's Front-Cover Text is:



   <p>A GNU Manual



   <p>(b) The FSF's Back-Cover Text is:



   <p>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. 

-->

</head>

<body>

<div class="node">

<p>

Node:<a name="Computed%20Includes">Computed Includes</a>,

Next:<a rel="next" accesskey="n" href="Wrapper-Headers.html#Wrapper%20Headers">Wrapper Headers</a>,

Previous:<a rel="previous" accesskey="p" href="Once-Only-Headers.html#Once-Only%20Headers">Once-Only Headers</a>,

Up:<a rel="up" accesskey="u" href="Header-Files.html#Header%20Files">Header Files</a>

<hr><br>

</div>



<h3 class="section">Computed Includes</h3>



   <p>Sometimes it is necessary to select one of several different header

files to be included into your program.  They might specify

configuration parameters to be used on different sorts of operating

systems, for instance.  You could do this with a series of conditionals,



<pre class="example">     #if SYSTEM_1

     # include "system_1.h"

     #elif SYSTEM_2

     # include "system_2.h"

     #elif SYSTEM_3

     ...

     #endif

     </pre>



   <p>That rapidly becomes tedious.  Instead, the preprocessor offers the

ability to use a macro for the header name.  This is called a

<dfn>computed include</dfn>.  Instead of writing a header name as the direct

argument of <code>#include</code>, you simply put a macro name there instead:



<pre class="example">     #define SYSTEM_H "system_1.h"

     ...

     #include SYSTEM_H

     </pre>



<p><code>SYSTEM_H</code> will be expanded, and the preprocessor will look for

<code>system_1.h</code> as if the <code>#include</code> had been written that way

originally.  <code>SYSTEM_H</code> could be defined by your Makefile with a

<code>-D</code> option.



   <p>You must be careful when you define the macro.  <code>#define</code> saves

tokens, not text.  The preprocessor has no way of knowing that the macro

will be used as the argument of <code>#include</code>, so it generates

ordinary tokens, not a header name.  This is unlikely to cause problems

if you use double-quote includes, which are close enough to string

constants.  If you use angle brackets, however, you may have trouble.



   <p>The syntax of a computed include is actually a bit more general than the

above.  If the first non-whitespace character after <code>#include</code> is

not <code>"</code> or <code>&lt;</code>, then the entire line is macro-expanded

like running text would be.



   <p>If the line expands to a single string constant, the contents of that

string constant are the file to be included.  CPP does not re-examine the

string for embedded quotes, but neither does it process backslash

escapes in the string.  Therefore



<pre class="example">     #define HEADER "a\"b"

     #include HEADER

     </pre>



<p>looks for a file named <code>a\"b</code>.  CPP searches for the file according

to the rules for double-quoted includes.



   <p>If the line expands to a token stream beginning with a <code>&lt;</code> token

and including a <code>&gt;</code> token, then the tokens between the <code>&lt;</code> and

the first <code>&gt;</code> are combined to form the filename to be included. 

Any whitespace between tokens is reduced to a single space; then any

space after the initial <code>&lt;</code> is retained, but a trailing space

before the closing <code>&gt;</code> is ignored.  CPP searches for the file

according to the rules for angle-bracket includes.



   <p>In either case, if there are any tokens on the line after the file name,

an error occurs and the directive is not processed.  It is also an error

if the result of expansion does not match either of the two expected

forms.



   <p>These rules are implementation-defined behavior according to the C

standard.  To minimize the risk of different compilers interpreting your

computed includes differently, we recommend you use only a single

object-like macro which expands to a string constant.  This will also

minimize confusion for people reading your program.



   </body></html>



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -