Makefile 报告 `func[abi:cxx11]()' 的多重定义

Makefile reporting multiple definition of `func[abi:cxx11]()'

我的教授希望我们在使用 IDE 之前使用终端编译和 运行 一些 c++ 代码,我在为其创建 Makefile 时遇到了一些问题。我今天去他的办公室求助,但他的回答含糊不清。我一直在网上四处寻找,找到了一个有用的视频,但我遇到了一个错误,我不知道如何修复

我尝试使用文本编辑器编辑 Makefile:

all:
    g++ main.cpp myfunc.cpp -o myexe

然后我进入终端 window 并输入:

$ make    

我收到的错误是:

g++ main.cpp myfunc.cpp -o myexe
/tmp/ccjSAvna.o: In function `func[abi:cxx11]()':
myfunc.cpp:(.text+0x0): multiple definition of `func[abi:cxx11]()'
/tmp/cczKi1CW.o:main.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'all' failed
make: *** [all] Error 1

我不太确定此错误消息的含义或如何解决我的问题。

I'm not really sure what this error message means [...]

一旦您意识到每一行的来源,它就相当简单了。第一行是 make 从您的 makefile 生成的命令:

g++ main.cpp myfunc.cpp -o myexe

有点奇怪,但很实用。通常 makefile 会单独编译每个源文件,然后 link 一起编译生成的目标文件,但是一步完成所有事情是可能的。

接下来的几行是g++的输出:

/tmp/ccjSAvna.o: In function `func[abi:cxx11]()':
myfunc.cpp:(.text+0x0): multiple definition of `func[abi:cxx11]()'
/tmp/cczKi1CW.o:main.cpp:(.text+0x0): first defined here

这些表明您的代码中存在错误。由于这与您的 makefile 无关,我将继续下一行。

collect2: error: ld returned 1 exit status

这一行只是显示前面命令的结果。这是 linker 错误后应该看到的内容。

Makefile:2: recipe for target 'all' failed
make: *** [all] Error 1

这些行是来自 make 的诊断信息,告诉您构建失败的时间。尝试 运行 您为 "all" 定义的命令时失败。在这种情况下有点多余,但是当您处理更复杂的 makefile 时,知道哪个规则失败可以节省时间。

[...] or how I can go about fixing my problem.

您可能希望在查找 makefile 中的问题之前调试您的代码。