c++ - 编译c++文件时出错: Undefined symbols

标签 c++ linker g++

我正在尝试编译其他人的 C++ 代码。我自己有 0 个 C++ 经验。我在 MAC 上使用 g++ 来编译我收到的其中一个 .cpp 文件。当我执行 g++ main.cpp 时,出现 undefined symbol 错误。谷歌搜索时,答案似乎与文件之间的错误链接有关,但我不知道如何链接文件。 我怎样才能得到要编译的文件?我在下面粘贴了完整的代码。

Undefined symbols:
  "initializeFitness()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Grid::GetNeighbourhood(int, int, std::vector<Agent**, std::allocator<Agent**> >&)", referenced from:
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
  "Grid::Grid()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Reaper::GetAgentToKill()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Cupid::GetRandomBreeder()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Reaper::Select()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Grid::~Grid()", referenced from:
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
  "Breeder::Breed(Agent**, Agent**)", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Cupid::Select()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Cupid::GetEmptyCell()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Agent::GetType()", referenced from:
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
  "Breeder::ProcessNeighbourhood(std::vector<Agent**, std::allocator<Agent**> > const&)", referenced from:
      runEvolution()     in ccZXBTDH.o
  "FateAgent::GetGenome(double*)", referenced from:
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
  "Cupid::GetParents()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Agent::IncreaseAge()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Grid::GetAgent(int, int)", referenced from:
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
  "FateAgent::GetSelectedCount()", referenced from:
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
  "Breeder::GetGenome(double*)", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Agent::GetFitness()", referenced from:
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
  "Agent::~Agent()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Grid::DoMovement()", referenced from:
      runEvolution()     in ccZXBTDH.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

最佳答案

我认为您只是试图编译整个程序的一部分。尝试 g++ *.cpp
如果还是不行,可能是因为你的程序需要一个外部库。

关于c++ - 编译c++文件时出错: Undefined symbols,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12748996/

相关文章:

c++ - 为什么需要空构造函数?

c++ - 将 vector 地址分配给迭代器

c++ - 有没有办法检测内联函数 ODR 违规?

c++ - 类构造函数 : unresolved externals

C++悬挂指针/深拷贝/浅拷贝混淆

c++ - 创建类的可变包装器

使用系统(命令)时,C++ 代码在 AWS 中失败,错误为 "cannot allocate memory"

c++ - 不兼容的 var 类型

c++ - 如何构建我的 Linux c++ 应用程序以链接到旧版本的 libc?

c - Openssl 未解析的外部 BN_is_zero