【发布时间】:2021-05-13 11:46:00
【问题描述】:
我正在尝试在 VS Code 中构建一个 c++ 项目,但是当我尝试构建它时,g++ 会抛出一个错误:
g++ -std=c++17 -ggdb -Iinclude src/main.cpp -o bin/main
Undefined symbols for architecture x86_64:
"MessageBus::MessageBus()", referenced from:
_main in main-244f95.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [bin/main] Error 1
The terminal process "/bin/zsh '-c', 'make'" terminated with exit code: 2.
以下是我认为导致问题的文件:
MessageBus.h
#pragma once
#include "../Utils/Queue.h"
#include "../Utils/SimpleList.h"
#include "Messages/Message.h"
class System;
class MessageBus
{
public:
MessageBus();
~MessageBus();
void addReciever(System* system);
void postMessage(Message* msg);
void notify();
private:
Queue<Message> msgQueue;
SimpleList<System*> systems;
};
MessageBus.cpp
#include "MessageBus.h"
#include "System.h"
MessageBus::MessageBus() {}
MessageBus::~MessageBus() {}
void MessageBus::postMessage(Message* msg) {
msgQueue.add(msg);
}
void MessageBus::addReciever(System* system) {
systems.add(system);
}
void MessageBus::notify() {
int queueLength = msgQueue.getLength();
for (int i = 0; i < queueLength; i++) {
Message msg = msgQueue.pop();
for (int j = 0; j < systems.getLength(); j++) {
System* system = systems.get(j);
system->handleMessage(&msg);
}
}
}
main.cpp
#include "EventSystem/MessageBus.h"
int main(int argc, char* argv[])
{
MessageBus* msgBus = new MessageBus();
}
生成文件
CXX := g++
CXX_FLAGS := -std=c++17 -ggdb
BIN := bin
SRC := src
INCLUDE := include
LIBRARIES :=
EXECUTABLE := main
all: $(BIN)/$(EXECUTABLE)
run: clean all
clear
./$(BIN)/$(EXECUTABLE)
$(BIN)/$(EXECUTABLE): $(SRC)/*.cpp
$(CXX) $(CXX_FLAGS) -I$(INCLUDE) $^ -o $@ $(LIBRARIES)
clean:
-rm $(BIN)/*
但是当我尝试使用终端编译这些文件时:
g++ main.cpp EventSystem/MessageBus.cpp -o maintest
它工作得很好,所以我认为问题是我的文件没有一起编译。我认为这可能与链接器无法找到正确的文件有关,并且可能与我的项目结构有关? This is my current structure
如您所见,头文件与源代码位于一起。我应该将头文件与 cpp 文件分开还是将它们放在子目录中?还是完全是别的东西?我对 c++ 和 Makefiles 有点陌生,我似乎无法理解导致问题的原因。
编辑:
解决方案:
正如@MadScientist 所建议的,我将Makefile 中的$(SRC)/*.cpp 替换为$(shell find $(SRC) -name \*.cpp -print),从而解决了问题。但正如@WhozCraig 提到的,我可能应该切换到 cmake 以避免将来使用 Makefile。
【问题讨论】:
-
请在您的问题中显示调用的链接命令。这是我们需要看到的最关键的事情,而你没有展示出来。
-
或者使用 cmake 并停止使用 makefile 杀死自己。不要误会我的意思;学习 makefile 是一项巨大的资产;随着更现代的项目管理工具的出现,它也越来越少被使用。
-
停止使用 Makefiles? CMake 的结果是什么?生成文件
标签: c++ visual-studio-code makefile g++ linker-errors