C 中的 #line 关键字
我试图理解一些代码,但遇到了一个我以前从未见过的关键字。我尝试用谷歌搜索它,但也没有找到任何有关它的信息。
char *valtext;
#line 1 "Values.l"
#define INITIAL 0
#line 2 "Values.l"
int reserve(char *s);
#line 388 "lex.val.c"
我已经包含了整个块,希望有人可以帮助我理解这段代码。我在系统上找不到任何名为“Values.l”的文件,并且这段代码位于“lex.val.c”文件中。
提前致谢。
I am trying to understand some code and I've come across a keyword that I've never seen before. I tried to google it, but haven't found anything regarding it as well.
char *valtext;
#line 1 "Values.l"
#define INITIAL 0
#line 2 "Values.l"
int reserve(char *s);
#line 388 "lex.val.c"
I've included the entire block hoping that perhaps someone can help me understand this chunk of code. I can't find any files on my system named "Values.l" and this chunk of code is located in the "lex.val.c" file.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
#line
指令用于使用预处理器,以便将源代码的原始行号传达给 C 编译器。它使得来自编译器的错误消息正确地引用用户能够理解的行号。例如,mycode.c 的第 12 行可能经过预处理器,现在是 mycode.tmp.cc 的第 183 行。如果 C 编译器在该行发现错误,您不希望被告知错误位于 mycode.tmp.cc 的第 183 行。所以C编译器需要给出每行的“原始坐标”。
#line
指令执行此操作,告诉编译器要在错误消息中使用的当前行号和文件名。The
#line
directive is for the use of preprocessors, so that the original line number of the source can be communicated to the C compiler. It makes it so that error messages from the compiler properly refer to line numbers the user will understand.For example, line 12 of your mycode.c may go through a preprocessor, and now be line 183 of mycode.tmp.cc. If the C compiler finds an error on that line, you don't want to be told the error is on line 183 of mycode.tmp.cc. So the C compiler needs to be given "original coordinates" of each line. The
#line
directive does this, telling the compiler the current line number and filename to use in error messages.行指令:
http://msdn.microsoft.com/en -US/library/b5w2czay%28v=VS.80%29.aspx
The line directive:
http://msdn.microsoft.com/en-US/library/b5w2czay%28v=VS.80%29.aspx
该代码已通过预处理器,因此由编译器的一个阶段标记,旨在由同一编译器的另一阶段使用。它使用的功能并不适合您使用。
它引用的文件可能是编译器运行时创建的临时文件。
That code has gone through the pre-processor and as such is marked up by one stage of a compiler, intended to be consumed by another stage of the same compiler. The features that it uses aren't intended for your use.
The files that it references may be temporary files created by the compiler as it runs.
这样做是为了行号发生变化。
这样做是为了显示 Lex 输入文件的行号,例如在错误消息和警告中。由于 Lex 生成 C 代码,因此如果没有
#line
指令,编译错误和警告将没有任何价值。This is done so that the line number changes.
This is done in order to show the line numbers of the Lex input file, for example, in error messages and warnings. Because Lex generates C code, without
#line
directives compile errors and warnings wouldn't be of any value.#line
指令设置编译器对当前文件名和行号的设置。这会影响__FILE__
和__LINE__
符号、失败的assert()
生成的输出以及诊断消息(错误和警告)。它通常由预处理器使用,以便错误和警告消息可以引用原始源代码,而不是预处理器的输出(通常在您看到任何消息时会被丢弃)。它也被其他生成 C 源代码的工具使用,例如 lex/flex 和 yacc/bison,因此错误消息可以引用输入文件而不是(临时)生成的 C 代码。
明确的参考是C 标准 (pdf ),第 6.10.4 节。
表单的一行
设置当前行号。表单的一行
设置行号和文件名。您还可以通过宏扩展生成这两种形式之一;例如:
A
#line
directive sets the compiler's setting for the current file name and line number. This affects the__FILE__
and__LINE__
symbols, the output generated by a failingassert()
, and diagnostic messages (errors and warnings). It's typically used by the preprocessor so that error and warning messages can refer to the original source code, not to the output of the preprocessor (which is typically discarded by the time you see any messages).It's also used by other tools that generate C source code, such as lex/flex and yacc/bison, so that error messages can refer to the input file rather than the (temporary) generated C code.
The definitive reference is the C standard (pdf), section 6.10.4.
A line of the form
sets the current line number. A line of the form
sets both the line number and the file name. You can also generate one of these two forms via macro expansion; for example: