由于我的代码有点太长,我想如果有人愿意帮助我并且需要代码的话,发布一个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.y
的 requires
部分中,它们的内容最终同时出现在 lex.yy.c
和 step3.tab.c
,所以一切都被定义了两次。
相反,您应该包含头文件,而不是 C 文件,然后通过将它们传递给 gcc 来编译和链接 ast.c
和 symbtable.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/