compiler-construction - 弹性/Bison : Multiple definition of 'said function'

标签 compiler-construction bison flex-lexer c-minus-minus

由于我的代码有点太长,我想如果有人愿意帮助我并且需要代码的话,发布一个github链接会更容易:https://github.com/Pigums/Cminus-Compiler

在 cygwin 中,我运行这些命令:

bison -d step3.y
flex step3.fl
gcc step3.tab.c lex.yy.c -lfl -o step3

然后弹出如下错误:

/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x0): multiple definition of `CreateTemp'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x0): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x4a): multiple definition of `Insert'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x4a): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x140): multiple definition of `PrintSym'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x140): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x19f): multiple definition of `Display'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x19f): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x1e6): multiple definition of `Search'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x1e6): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x266): multiple definition of `Delete'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x266): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x2fd): multiple definition of `ASTCreateNode'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x2fd): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x3c0): multiple definition of `ASTattachleft'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x3c0): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x3f6): multiple definition of `PT'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x3f6): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0x427): multiple definition of `ASTprint'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0x427): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.text+0xa83): multiple definition of `compareFormals'
/tmp/ccfXBuoP.o:lex.yy.c:(.text+0xa83): first defined here
/tmp/ccdKHQL3.o:step3.tab.c:(.bss+0x0): multiple definition of `mem'
/tmp/ccfXBuoP.o:lex.yy.c:(.bss+0x14): first defined here
collect2: error: ld returned 1 exit status

不确定我做错了什么,尝试查找错误,但我认为我得到的答案不是我正在寻找的答案。这里有什么问题?

最佳答案

#include "symtable.c"
#include "ast.c"

这就是你的问题。通过将这两个 C 文件包含在 step3.yrequires 部分中,它们的内容最终同时出现在 lex.yy.cstep3.tab.c,所以一切都被定义了两次。

相反,您应该包含头文件,而不是 C 文件,然后通过将它们传递给 gcc 来编译和链接 ast.csymbtable.c:

gcc step3.tab.c lex.yy.c ast.c symtable.c -o step3

(也可以使用Makefile分别编译每个文件,然后将它们链接在一起,这样只需要重新编译发生变化的文件,但那是完全不同的事情)

请注意,这并非特定于 flex 或 bison。你不应该 #include C 文件,除非你确切地知道这意味着什么并且你有一个很好的理由。

关于compiler-construction - 弹性/Bison : Multiple definition of 'said function' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53254160/

相关文章:

c++ - mingw 编译器错误

c++ - 指向不完整类型的指针

c - 避免警告 : unreferenced find_rule label

c++ - 如何为 flex/bison 实现更好的错误消息

python - 检测 Python 或动态语言中的无限递归

assembly - 如何在 MIPS 中在堆栈上推送和弹出地址

c - yylineno 给出错误报告的意外结果

linux - 编译错误 : No suitable bison/yacc found

c - parser.y :79. 33-41 : symbol character is used, 但未定义为 token 且没有规则

cmake - 如何为 flex & bison 编写一个有效的 cmake 文件?