【问题标题】:What is the meaning of lines starting with a hash sign and number like '# 1 "a.c"' in the gcc preprocessor output?在 gcc 预处理器输出中,以井号和数字(如 '# 1 "a.c"' )开头的行是什么意思?
【发布时间】:2011-07-19 05:42:37
【问题描述】:

我通过使用打印出 C 预处理器的输出

gcc -E a.c

输出包含许多行,如

# 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

我在 C 中从未见过这种语法。有人能解释一下这是做什么的吗?

【问题讨论】:

标签: c gcc c-preprocessor


【解决方案1】:

这些行是调试提示(该行后面的代码实际来自哪里)

# line-number "source-file" [flags]

标志的含义(空格分隔):

  • 1 - 新文件的开始
  • 2 - 返回上一个文件
  • 3 - 以下文本来自系统头文件(#include vs #include "")
  • 4 - 以下文本应被视为包含在隐式外部“C”块中。

【讨论】:

【解决方案2】:

man gcc 中针对-P 选项提到了这些线标记

-P 选项专门用于删除这些行以清楚起见:

gcc -E -P source.c

detailed documentation(之前已回答)。

【讨论】:

    【解决方案3】:

    这些是行同步指令,允许gcc#included 文件中的错误提供正确的错误消息。其他预处理器(例如 yacc/bison)使用相同的机制将 C 错误与输入 .y 文件中的正确行关联起来。

    【讨论】:

    • 不,其他 C 代码生成器,如 bison 应该发出 #line 预处理器指令......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 2010-12-02
    • 2014-05-30
    相关资源
    最近更新 更多