2

由于我的代码有点太长,如果有人愿意帮助我并需要代码,我认为发布 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

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

4

3 回答 3

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

那就是你的问题。通过将这两个 C 文件包含在 的requires部分中step3.y,它们的内容都以lex.yy.cand结尾step3.tab.c,因此所有内容都被定义了两次。

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

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

(你也可以使用 aMakefile单独编译每个文件,然后将它们链接在一起,所以你只需要重新编译已经改变的文件,但那是完全不同的事情)

请注意,这并不特定于 flex 或 bison。#include除非您确切知道这意味着什么并且您有充分的理由,否则您永远不应该使用 C 文件。

于 2018-11-11T23:18:07.743 回答
2

这可能会迟到,但我必须把它留在这里。即使我使用了正确的包含语句,我也有相同的“函数问题的多重定义”。我不太清楚为什么会发生这种情况,但是我只在我的头文件中为全局函数(不在任何类中)获得了多个定义错误。我有一堆实用方法,成为一个类的成员函数并没有多大意义。

所以我得到了这些功能的“多重定义”错误:

我的问题

所以我的解决方案是:

解决方案

基本上用一个类把它们包起来解决了这个问题,现在它工作得很好。

于 2019-07-11T12:48:16.693 回答
0

我尝试了两种解决方案。他们工作得很好。但是在我使用类的其他头文件中,我仍然有更多的“多重定义”错误。这些文件在类内有方法声明,但在类外有使用Scope resolution(::)的方法定义

所以我尝试了这个

删除所有方法声明并在类中写入所有方法定义

它奏效了!

于 2019-07-16T16:14:48.350 回答