【问题标题】:Why am I getting "ld: symbol(s) not found for architecture x86_64" [duplicate]为什么我得到“ld:未找到架构 x86_64 的符号”[重复]
【发布时间】:2021-01-02 08:06:27
【问题描述】:

我是 C++ 的完全初学者。我正在通过object oriented programming Data structures in c++ 学习 C++。在课程中我有以下程序

Cube.h

#pragma once

class Cube {
  public:
    double getVolume();
    double getSurfaceArea();
    void setLength(double length);

  private:
    double length_;
};

Cube.cpp

#include "Cube.h"

double Cube::getVolume() {
  return length_ * length_ * length_;
}

double Cube::getSurfaceArea() {
  return 6 * length_ * length_;
}

void Cube::setLength(double length) {
  length_ = length;
}

main.cpp

#include <iostream>
#include "Cube.h"

int main() {
  Cube c;

  c.setLength(3.48);
  double volume = c.getVolume();

  std::cout << "Volume" << volume << std::endl;

  return 0;
}

当我使用make main 制作这个程序时,我收到以下错误消息

c++     main.cpp   -o main
Undefined symbols for architecture x86_64:
  "Cube::getVolume()", referenced from:
      _main in main-6c5fe0.o
  "Cube::setLength(double)", referenced from:
      _main in main-6c5fe0.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: *** [main] Error 1

不知道我做错了什么。我正在使用 Macbook 工作。我跟着这个link在mac中运行cpp程序。

不知道我做错了什么。对错误的解释也很好。

提前致谢

【问题讨论】:

  • 你好,你也应该编译Cube.cpp,比如c++ *.cpp -o main。
  • this question 上的答案很有趣。出于某种原因,所有人都假设您编译单个源文件并且不适用于您的情况
  • @idclev463035818 抱歉我的无知。正如我在问题中解释的那样,我是完全的初学者,还不了解错误。现在已经学会了
  • @Sashaank 无需抱歉。也许答案的作者应该是;)

标签: c++


【解决方案1】:

你需要链接main.cppCube.cpp在一起,所以你必须编译:

c++ main.cpp Cube.cpp -o main

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 2018-02-20
    • 1970-01-01
    相关资源
    最近更新 更多