小编Wol*_*olf的帖子

当 GCC 的预处理器删除了注释时,它如何知道错误位置(在源代码中)?

我想知道当 GCC 的预处理器删除了注释时,它如何知道错误在哪里(在源代码中)?我用谷歌搜索了它,但找不到它。我将解释我的意思:

\n

我有这样的C代码:

\n
int main(void)\n{\n  return /* comment */ ) /* another comment */0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

\')\' 字符(第 24 个字符)位置存在语法错误。然后我通过 GCC 预处理器 ( gcc -E main.c) 对其进行过滤,结果是:

\n
# 0 "main.c"\n# 0 "<built-in>"\n# 0 "<command-line>"\n# 1 "/usr/include/stdc-predef.h" 1 3 4\n# 0 "<command-line>" 2\n# 1 "main.c"\nint main(void)\n{\n  return ) 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

好吧,我还是明白这些步骤。预处理器已删除注释。但事情是这样的。现在语法错误出现在第 10 个字符的位置(不是第 24 个字符),因为注释被删除了。那么它如何知道语法错误到底在哪里呢?(正如我们在下面的输出中看到的)

\n
main.c: In function \xe2\x80\x98main\xe2\x80\x99:\nmain.c:3:24: error: expected expression before \xe2\x80\x98)\xe2\x80\x99 token\n    3 |   return /* comment */ ) /* another comment */0;\n …
Run Code Online (Sandbox Code Playgroud)

c gcc c-preprocessor

10
推荐指数
1
解决办法
521
查看次数

标签 统计

c ×1

c-preprocessor ×1

gcc ×1