source: public/doc/gnu-c/Computed-Includes.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 6.8 KB
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
4
5(The work of Trevis Rothwell and Nelson Beebe has been assigned or
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing GNU and promoting software freedom." -->
19<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
20<head>
21<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
22<title>Computed Includes (GNU C Language Manual)</title>
23
24<meta name="description" content="Computed Includes (GNU C Language Manual)">
25<meta name="keywords" content="Computed Includes (GNU C Language Manual)">
26<meta name="resource-type" content="document">
27<meta name="distribution" content="global">
28<meta name="Generator" content="makeinfo">
29<link href="index.html" rel="start" title="Top">
30<link href="Symbol-Index.html" rel="index" title="Symbol Index">
31<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
32<link href="Header-Files.html" rel="up" title="Header Files">
33<link href="Macros.html" rel="next" title="Macros">
34<link href="Once_002dOnly-Headers.html" rel="prev" title="Once-Only Headers">
35<style type="text/css">
36<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.no-bullet {list-style: none}
51-->
52</style>
53
54
55</head>
56
57<body lang="en">
58<span id="Computed-Includes"></span><div class="header">
59<p>
60Previous: <a href="Once_002dOnly-Headers.html" accesskey="p" rel="prev">Once-Only Headers</a>, Up: <a href="Header-Files.html" accesskey="u" rel="up">Header Files</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
61</div>
62<hr>
63<span id="Computed-Includes-1"></span><h4 class="subsection">26.4.5 Computed Includes</h4>
64<span id="index-computed-includes"></span>
65<span id="index-macros-in-include"></span>
66
67<p>Sometimes it is necessary to select one of several different header
68files to be included into your program. They might specify
69configuration parameters to be used on different sorts of operating
70systems, for instance. You could do this with a series of conditionals,
71</p>
72<div class="example">
73<pre class="example">#if SYSTEM_1
74# include &quot;system_1.h&quot;
75#elif SYSTEM_2
76# include &quot;system_2.h&quot;
77#elif SYSTEM_3
78/* <span class="roman">&hellip;</span> */
79#endif
80</pre></div>
81
82<p>That rapidly becomes tedious. Instead, GNU C offers the ability to use
83a macro for the header name. This is called a <em>computed include</em>.
84Instead of writing a header name as the direct argument of
85<code>#include</code>, you simply put a macro name there instead:
86</p>
87<div class="example">
88<pre class="example">#define SYSTEM_H &quot;system_1.h&quot;
89/* <span class="roman">&hellip;</span> */
90#include SYSTEM_H
91</pre></div>
92
93<p><code>SYSTEM_H</code> is expanded, then <samp>system_1.h</samp> is included as if
94the <code>#include</code> had been written with that name. <code>SYSTEM_H</code>
95could be defined by your Makefile with a <samp>-D</samp> option.
96</p>
97<p>You must be careful when you define such a macro. <code>#define</code>
98saves tokens, not text. GCC has no way of knowing that the macro will
99be used as the argument of <code>#include</code>, so it generates ordinary
100tokens, not a header name. This is unlikely to cause problems if you
101use double-quote includes, which are syntactically similar to string
102constants. If you use angle brackets, however, you may have trouble.
103</p>
104<p>The syntax of a computed include is actually a bit more general than the
105above. If the first non-whitespace character after <code>#include</code> is
106not &lsquo;<samp>&quot;</samp>&rsquo; or &lsquo;<samp>&lt;</samp>&rsquo;, then the entire line is macro-expanded
107like running text would be.
108</p>
109<p>If the line expands to a single string constant, the contents of that
110string constant are the file to be included. Preprocessing does not
111re-examine the string for embedded quotes, but neither does it process
112backslash escapes in the string. Therefore
113</p>
114<div class="example">
115<pre class="example">#define HEADER &quot;a\&quot;b&quot;
116#include HEADER
117</pre></div>
118
119<p>looks for a file named <samp>a\&quot;b</samp>. Preprocessing searches for the
120file according to the rules for double-quoted includes.
121</p>
122<p>If the line expands to a token stream beginning with a &lsquo;<samp>&lt;</samp>&rsquo; token
123and including a &lsquo;<samp>&gt;</samp>&rsquo; token, then the tokens between the &lsquo;<samp>&lt;</samp>&rsquo; and
124the first &lsquo;<samp>&gt;</samp>&rsquo; are combined to form the filename to be included.
125Any whitespace between tokens is reduced to a single space; then any
126space after the initial &lsquo;<samp>&lt;</samp>&rsquo; is retained, but a trailing space
127before the closing &lsquo;<samp>&gt;</samp>&rsquo; is ignored. Preprocessing searches for the file
128according to the rules for angle-bracket includes.
129</p>
130<p>In either case, if there are any tokens on the line after the file name,
131an error occurs and the directive is not processed. It is also an error
132if the result of expansion does not match either of the two expected
133forms.
134</p>
135<p>These rules are implementation-defined behavior according to the C
136standard. To minimize the risk of different compilers interpreting your
137computed includes differently, we recommend you use only a single
138object-like macro that expands to a string constant. That also
139makes it clear to people reading your program.
140</p>
141<hr>
142<div class="header">
143<p>
144Previous: <a href="Once_002dOnly-Headers.html" accesskey="p" rel="prev">Once-Only Headers</a>, Up: <a href="Header-Files.html" accesskey="u" rel="up">Header Files</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
145</div>
146
147
148
149</body>
150</html>
Note: See TracBrowser for help on using the repository browser.