C语言 gcc 预处理器输出中以井号和数字(如“#1“ac””)开头的行是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5370539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the meaning of lines starting with a hash sign and number like '# 1 "a.c"' in the gcc preprocessor output?
提问by user607722
I print out the output of C preprocessor by using
我通过使用打印出 C 预处理器的输出
gcc -E a.c
The output contains many lines like
输出包含许多行,如
# 1 "a.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "a.c"
# 1 "c:\mingw\bin\../lib/gcc/mingw32/4.5.0/../../../../include/stdio.h" 1 3
# 19 "c:\mingw\bin\../lib/gcc/mingw32/4.5.0/../../../../include/stdio.h" 3
# 1 "c:\mingw\bin\../lib/gcc/mingw32/4.5.0/../../../../include/_mingw.h" 1 3
# 31 "c:\mingw\bin\../lib/gcc/mingw32/4.5.0/../../../../include/_mingw.h" 3
# 32 "c:\mingw\bin\../lib/gcc/mingw32/4.5.0/../../../../include/_mingw.h" 3
# 20 "c:\mingw\bin\../lib/gcc/mingw32/4.5.0/../../../../include/stdio.h" 2 3
I never seen this kind of syntax in C. Can someone explain what this is doing?
我从未在 C 中见过这种语法。有人可以解释一下这是做什么的吗?
回答by jdehaan
These lines are hints for debugging (where the code following the line actually came from)
这些行是调试的提示(该行后面的代码实际上来自哪里)
# line-number "source-file" [flags]
Meaning of flags (space separated):
标志的含义(空格分隔):
- 1 - Start of a new file
- 2 - Returning to previous file
- 3 - Following text comes from a system header file (#include <> vs #include "")
- 4 - Following text should be treated as being wrapped in an implicit extern "C" block.
- 1 - 开始一个新文件
- 2 - 返回上一个文件
- 3 - 以下文本来自系统头文件(#include <> vs #include "")
- 4 - 应将以下文本视为包含在隐式 extern "C" 块中。
回答by uvsmtid
These linemarkersare mentioned in man gccfor -Poption.
这些linemarkers被中提到man gcc的-P选项。
The -Poption is specifically meant to get rid of these lines for clarity:
-P为了清楚起见,该选项专门用于删除这些行:
gcc -E -P source.c
See detailed documentation(answered before).
请参阅详细文档(之前已回答)。
回答by geekosaur
Those are line synchronization directives, which allow gccto give correct error messages for errors in #included files. Other preprocessors (such as yacc/bison) use the same mechanism to relate C errors to the correct lines in the input .yfile.
这些是行同步指令,允许gcc为#included 文件中的错误提供正确的错误消息。其他预处理器(例如yacc/ bison)使用相同的机制将 C 错误与输入.y文件中的正确行相关联。

