gcc 如何知道源代码来自哪里?
今天我正在玩 Flex 和 Bison,一些奇怪的东西引起了我的注意。
localhost:c math4tots$ lex c.l
localhost:c math4tots$ yacc -d c.y
localhost:c math4tots$ rm c.l c.y
localhost:c math4tots$ gcc c.c lex.yy.c y.tab.c
c.y: In function ‘opr’:
c.y:120: error: ‘nodeType’ has no member named ‘oper’
我只通过了 cc lex.yy.c 和 y.tab.c (ch 和 y.tab.h 也作为标头包含在内),但不知何故 gcc 知道 cl 和 cy 事实上,即使在我删除之后cl和cy,gcc知道cy中代码中的错误在哪里。它是如何做到的?
我觉得我在过去使用过的一些不同工具中看到过类似的东西,但我不记得它们是什么。
I was playing with flex and bison today, and something kind of eerie came to my attention.
localhost:c math4tots$ lex c.l
localhost:c math4tots$ yacc -d c.y
localhost:c math4tots$ rm c.l c.y
localhost:c math4tots$ gcc c.c lex.yy.c y.tab.c
c.y: In function ‘opr’:
c.y:120: error: ‘nodeType’ has no member named ‘oper’
I've only passed c.c lex.yy.c, and y.tab.c (c.h and y.tab.h are also included as headers), but somehow gcc knows about c.l and c.y. In fact, even after I've deleted c.l and c.y, gcc knows where in c.y the error in the code was. How does it do that?
I feel like I've seen something similar for some different tools I've used in the past, but I can't exactly remember what they were.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将在文件中找到一些标记,大致如下:
正是用于此目的,能够针对生成您正在编译的实际文件的原始文件报告错误消息或警告。
基本上,您不必转到 cc 文件并尝试找出必须修复的 cy 文件中的等效行。
从这个意义上说,C 文件与目标文件没有什么不同。您不必关心其中的内容,因为它是自动生成的。如果出现问题,您希望能够直接返回到 cy 中的正确行并从源头修复它。
You'll find some markers in your files along the lines of:
which is used for exactly this purpose, the ability to report error messages or warnings against the original file that produced the actual files you're compiling.
It's basically so that you don't have to go to the
c.c
file and try to figure out the equivalent line in thec.y
file that you have to fix.In this sense, the C file is no different to an object file. You don't care what's in it since it's automatically generated. If there's a problem with it, you want to be able to go directly back to the right line in
c.y
and fix it at the source.生成的 C 文件有一些 行控制预处理器指令(也生成),例如
然后编译器认为该指令之后的下一行位于文件 cy 的第 119 行,并相应地计算以下位置。
The generated C file has some line control preprocessor directives (also generated) like
and then the compiler believes that the next line after this directive is in file
c.y
at line 119 and compute following positions accordingly.