1<html lang="en"> 2<head> 3<title>Line Control - The C Preprocessor</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="The C Preprocessor"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="prev" href="Diagnostics.html#Diagnostics" title="Diagnostics"> 9<link rel="next" href="Pragmas.html#Pragmas" title="Pragmas"> 10<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 11<!-- 12Copyright (C) 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 131997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 142008, 2009, 2010, 2011 15Free Software Foundation, Inc. 16 17Permission is granted to copy, distribute and/or modify this document 18under the terms of the GNU Free Documentation License, Version 1.3 or 19any later version published by the Free Software Foundation. A copy of 20the license is included in the 21section entitled ``GNU Free Documentation License''. 22 23This manual contains no Invariant Sections. The Front-Cover Texts are 24(a) (see below), and the Back-Cover Texts are (b) (see below). 25 26(a) The FSF's Front-Cover Text is: 27 28 A GNU Manual 29 30(b) The FSF's Back-Cover Text is: 31 32 You have freedom to copy and modify this GNU Manual, like GNU 33 software. Copies published by the Free Software Foundation raise 34 funds for GNU development. 35--> 36<meta http-equiv="Content-Style-Type" content="text/css"> 37<style type="text/css"><!-- 38 pre.display { font-family:inherit } 39 pre.format { font-family:inherit } 40 pre.smalldisplay { font-family:inherit; font-size:smaller } 41 pre.smallformat { font-family:inherit; font-size:smaller } 42 pre.smallexample { font-size:smaller } 43 pre.smalllisp { font-size:smaller } 44 span.sc { font-variant:small-caps } 45 span.roman { font-family:serif; font-weight:normal; } 46 span.sansserif { font-family:sans-serif; font-weight:normal; } 47--></style> 48<link rel="stylesheet" type="text/css" href="../cs.css"> 49</head> 50<body> 51<div class="node"> 52<a name="Line-Control"></a> 53<p> 54Next: <a rel="next" accesskey="n" href="Pragmas.html#Pragmas">Pragmas</a>, 55Previous: <a rel="previous" accesskey="p" href="Diagnostics.html#Diagnostics">Diagnostics</a>, 56Up: <a rel="up" accesskey="u" href="index.html#Top">Top</a> 57<hr> 58</div> 59 60<h2 class="chapter">6 Line Control</h2> 61 62<p><a name="index-line-control-98"></a> 63The C preprocessor informs the C compiler of the location in your source 64code where each token came from. Presently, this is just the file name 65and line number. All the tokens resulting from macro expansion are 66reported as having appeared on the line of the source file where the 67outermost macro was used. We intend to be more accurate in the future. 68 69 <p>If you write a program which generates source code, such as the 70<samp><span class="command">bison</span></samp> parser generator, you may want to adjust the preprocessor's 71notion of the current file name and line number by hand. Parts of the 72output from <samp><span class="command">bison</span></samp> are generated from scratch, other parts come 73from a standard parser file. The rest are copied verbatim from 74<samp><span class="command">bison</span></samp>'s input. You would like compiler error messages and 75symbolic debuggers to be able to refer to <code>bison</code>'s input file. 76 77 <p><a name="index-g_t_0023line-99"></a><samp><span class="command">bison</span></samp> or any such program can arrange this by writing 78‘<samp><span class="samp">#line</span></samp>’ directives into the output file. ‘<samp><span class="samp">#line</span></samp>’ is a 79directive that specifies the original line number and source file name 80for subsequent input in the current preprocessor input file. 81‘<samp><span class="samp">#line</span></samp>’ has three variants: 82 83 <dl> 84<dt><code>#line </code><var>linenum</var><dd><var>linenum</var> is a non-negative decimal integer constant. It specifies 85the line number which should be reported for the following line of 86input. Subsequent lines are counted from <var>linenum</var>. 87 88 <br><dt><code>#line </code><var>linenum</var> <var>filename</var><dd><var>linenum</var> is the same as for the first form, and has the same 89effect. In addition, <var>filename</var> is a string constant. The 90following line and all subsequent lines are reported to come from the 91file it specifies, until something else happens to change that. 92<var>filename</var> is interpreted according to the normal rules for a string 93constant: backslash escapes are interpreted. This is different from 94‘<samp><span class="samp">#include</span></samp>’. 95 96 <p>Previous versions of CPP did not interpret escapes in ‘<samp><span class="samp">#line</span></samp>’; 97we have changed it because the standard requires they be interpreted, 98and most other compilers do. 99 100 <br><dt><code>#line </code><var>anything else</var><dd><var>anything else</var> is checked for macro calls, which are expanded. 101The result should match one of the above two forms. 102</dl> 103 104 <p>‘<samp><span class="samp">#line</span></samp>’ directives alter the results of the <code>__FILE__</code> and 105<code>__LINE__</code> predefined macros from that point on. See <a href="Standard-Predefined-Macros.html#Standard-Predefined-Macros">Standard Predefined Macros</a>. They do not have any effect on ‘<samp><span class="samp">#include</span></samp>’'s 106idea of the directory containing the current file. This is a change 107from GCC 2.95. Previously, a file reading 108 109<pre class="smallexample"> #line 1 "../src/gram.y" 110 #include "gram.h" 111</pre> 112 <p>would search for <samp><span class="file">gram.h</span></samp> in <samp><span class="file">../src</span></samp>, then the <samp><span class="option">-I</span></samp> 113chain; the directory containing the physical source file would not be 114searched. In GCC 3.0 and later, the ‘<samp><span class="samp">#include</span></samp>’ is not affected by 115the presence of a ‘<samp><span class="samp">#line</span></samp>’ referring to a different directory. 116 117 <p>We made this change because the old behavior caused problems when 118generated source files were transported between machines. For instance, 119it is common practice to ship generated parsers with a source release, 120so that people building the distribution do not need to have yacc or 121Bison installed. These files frequently have ‘<samp><span class="samp">#line</span></samp>’ directives 122referring to the directory tree of the system where the distribution was 123created. If GCC tries to search for headers in those directories, the 124build is likely to fail. 125 126 <p>The new behavior can cause failures too, if the generated file is not 127in the same directory as its source and it attempts to include a header 128which would be visible searching from the directory containing the 129source file. However, this problem is easily solved with an additional 130<samp><span class="option">-I</span></samp> switch on the command line. The failures caused by the old 131semantics could sometimes be corrected only by editing the generated 132files, which is difficult and error-prone. 133 134 </body></html> 135 136