gpt4 book ai didi

c++ - 奇怪的错误 "multiple definition of ` xxx`“编译 C++ 项目时

转载 作者:行者123 更新时间:2023-11-30 01:22:03 26 4
gpt4 key购买 nike

当我尝试通过我的 Makefile 编译我的 C++ 项目时,我不断收到类似这样的错误:

Server.o: In function `Bot::getRandomMessage()':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:18: multiple definition of `Bot::getRandomMessage()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:18: first defined here
Server.o: In function `Bot':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:27: multiple definition of `Bot::Bot()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:27: first defined here
Server.o: In function `~Bot':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: multiple definition of `Bot::~Bot()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: first defined here
Server.o: In function `~Bot':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: multiple definition of `Bot::~Bot()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: first defined here
Server.o: In function `~Bot':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: multiple definition of `Bot::~Bot()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:30: first defined here
Server.o: In function `Bot::getName()':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:33: multiple definition of `Bot::getName()'
Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:33: first defined here
Server.o: In function `ChatRoom::getCurrentTime()':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Server.cpp:74: multiple definition of `ChatRoom::getCurrentTime()'
main.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Server.cpp:74: first defined here
Server.o: In function `Bot::getRandomMessage()':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:18: multiple definition of `vectorOfThreads'
main.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Server.cpp:74: first defined here
Server.o: In function `Bot::getRandomMessage()':

我对此很困惑..当我直接用命令编译它时g++ main.cpp -Wall -pedantic -Wno-long-long -O0 -ggdb -lncurses -pthread -o AppName ,那么它不会产生任何错误。所以我预计错误会出现在我的 Makefile

中的某处
#macros
Remove=rm -rf
Doxygen=Doxyfile
RUN=./dvoram64
FLAGS=-Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g
OBJECTS=main.o Bot.o Server.o Client.o GrumpyBot.o JokerBot.o WeatherBot.o DummyBot.o

#generates final binary and documentation
all: $(Doxygen)
make compile

#build into final binary
compile: $(RUN)

#run program
run:
make link
$(RUN)
$(RUN)


clean:
$(Remove) dvoram64
$(Remove) $(OBJECTS)

#generate documentation in '<login>/doc' folder
doc: $(Doxygen) /*
( cd ./ | doxygen $(Doxygen))

link: $(OBJECTS)
g++ $(OBJECTS) -lncurses -pthread -o dvoram64

#rules how to compile into the executalble file
$(RUN): $(OBJECTS)

Bot.o: ./Bot.cpp ./Bot.h
g++ $(FLAGS) -c ./Bot.cpp

DummyBot.o: ./DummyBot.cpp ./DummyBot.h ./Bot.h
g++ $(FLAGS) -c ./DummyBot.cpp

GrumpyBot.o: ./GrumpyBot.cpp ./GrumpyBot.h ./Bot.h
g++ $(FLAGS) -c ./GrumpyBot.cpp

JokerBot.o: ./JokerBot.cpp ./JokerBot.h ./Bot.h
g++ $(FLAGS) -c ./JokerBot.cpp

WeatherBot.o: ./WeatherBot.cpp ./WeatherBot.h ./Bot.h
g++ $(FLAGS) -c ./WeatherBot.cpp

Client.o: ./Client.cpp
g++ $(FLAGS) -c ./Client.cpp


main.o: ./main.cpp
g++ $(FLAGS) -c ./main.cpp

Server.o: ./Server.cpp ./Bot.h ./JokerBot.h ./WeatherBot.h ./GrumpyBot.h ./DummyBot.h
g++ $(FLAGS) -c ./Server.cpp

谁能解释一下,是什么导致了这个错误,并告诉我如何解决它?

最佳答案

查看错误消息告诉您的内容。从第一行开始:

Server.o: In function `Bot::getRandomMessage()':
/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:18: multiple definition of `Bot::getRandomMessage()'

此消息表示目标文件 Server.o 包含函数 Bot::getRandomMessage() 函数的多重定义,并且多重定义来自行源文件 Bot.cpp 中的 18。现在看下一行:

Bot.o:/home/ubuntu/NetBeansProjects/SemestralniPraceChat/./Bot.cpp:18: 首先在这里定义

这告诉你 Server.o 中的定义是一个多重定义,因为 Bot.o 中也有一个定义。它还告诉您 Bot.o 中的定义来自源文件 Bot.cpp 中的第 18 行,与其他定义在源文件中的相同位置。

这意味着 Bot.cpp 至少编译了两次,一次生成 Server.o 一次生成 Bot.o.

这可能不是您想要的。它表明您的某些源文件或头文件包含 Bot.cpp,而您打算包含 Bot.h,或者您以其他方式包含 Bot.cpp 错误。另一种可能性是您有一个编译命令,该命令将 Bot.cpp 编译为 Server.o

关于c++ - 奇怪的错误 "multiple definition of ` xxx`“编译 C++ 项目时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17011334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com