C++ 体系结构 x86_64 的未定义符号:编译问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18751868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:11:16  来源:igfitidea点击:

Undefined Symbols for architecture x86_64: Compiling problems

c++hyperlinkcompilationundefinedsymbols

提问by Trevor Hutto

So I am trying to start an assignment, my professor gives us a Main.cpp, Main.h, Scanner.cpp, Scanner.h, and some other utilities.

所以我正在尝试开始一项作业,我的教授给了我们一个 Main.cpp、Main.h、Scanner.cpp、Scanner.h 和其他一些实用程序。

My job is to create a Similarity class to compare documents using the cosine and Jaccard coefficients. However, I can not seem to get the project linked correctly, therefore I am unable to start on the actual code.

我的工作是创建一个 Similarity 类来使用余弦和 Jaccard 系数比较文档。但是,我似乎无法正确链接项目,因此我无法开始实际代码。

After trying for several hours to see what I am doing wrong, I need fresh eyes to see what I am doing wrong, I suspect it is obvious.

在尝试了几个小时以查看我做错了什么之后,我需要新的眼睛来查看我做错了什么,我怀疑这很明显。

Here is the Main.cpp

这是 Main.cpp

#include "Main.h"

using namespace std;

static const string TAG = "Main: ";

int main(int argc, char *argv[])
{
  string inStreamName;
  string logStreamName;
  string outStreamName;

  ofstream outStream;
  string timeCallOutput;
  Scanner inStream;

  Similarity similarity;

  ///////////////////////////////////////////////////////////////
  // Boilerplate for naming files and opening files
  Utils::CheckArgs(3, argc, argv, "infilename outfilename logfilename");
  outStreamName = static_cast<string>(argv[2]);
  logStreamName = static_cast<string>(argv[3]);

  Utils::FileOpen(outStream, outStreamName);
  Utils::LogFileOpen(logStreamName);

  timeCallOutput = Utils::timecall("beginning");
  Utils::logStream << timeCallOutput << endl;
  Utils::logStream << TAG << "Beginning execution" << endl;

  Utils::logStream << TAG << "outfile '" << outStreamName << "'" << endl;
  Utils::logStream << TAG << "logfile '" << logStreamName << "'" << endl;
  Utils::logStream.flush();

  ///////////////////////////////////////////////////////////////
  // What follows here is the real work of this code.
  //   read the entire input file and echo it back
  //   compute the two similarity coefficients

  inStreamName = static_cast<string>(argv[1]);
  inStream.openFile(inStreamName);
  Utils::logStream << TAG << "infile '" << inStreamName << "'" << endl;
  Utils::logStream.flush();

  similarity.readData(inStream);

  outStream << TAG << "Data Begin\n" << similarity.toString() << endl;
  outStream << TAG << "Data End\n" << endl;
  outStream.flush();
  inStream.close();

  outStream << TAG << "Begin similarity computation" << endl;
  outStream << TAG << "Maximum Jaccard similarity:\n" <<
                       similarity.maxJaccard() << endl;
  outStream << TAG << "Maximum cosine similarity:\n" <<
                       similarity.maxCosine() << endl;
  outStream << TAG << "End similarity computation" << endl;
  outStream.flush();

  ///////////////////////////////////////////////////////////////
  // Boilerplate for terminating gracefully
  Utils::logStream << TAG << "Ending execution" << endl;
  timeCallOutput = Utils::timecall("ending");
  Utils::logStream << timeCallOutput << endl;
  Utils::logStream.flush();

  outStream.flush();
  Utils::FileClose(outStream);

  Utils::FileClose(Utils::logStream);

  return 0;
}

And the Main.h

和 Main.h

#ifndef MAIN_H
#define MAIN_H

#include "../../Utilities/Utils.h"
#include "../../Utilities/Scanner.h"

#include "Similarity.h"

class Main
{
public:
  int main();
  virtual ~Main();

private:

};

#endif // MAIN_H

My Similarity.cpp

我的相似度.cpp

#include "Similarity.h"

using namespace std;

void readData(Scanner& inStream){
}

string maxCosine(){
    return "cosine";
}

string maxJaccard(){
    return "jaccard";
}

string toString(){
    return "toString";
}

And finally my Similarity.h:

最后是我的 Similarity.h:

#ifndef SIMILARITY_H
#define SIMILARITY_H

#include "../../Utilities/Scanner.h"

class Similarity
{
public:
    Similarity();
    virtual ~Similarity();

    void readData(Scanner& inStream);
    string maxCosine();
    string maxJaccard();
    string toString();
private:

};

#endif

When I use the makefile he provided, and the one I have to use in order for his script to work to grade it I get this error:

当我使用他提供的 makefile 以及我必须使用的 makefile 以便他的脚本对其进行评分时,我收到此错误:

g++ -O3 -Wall -o Similarity.o -c Similarity.cpp
g++ -O3 -Wall -o Aprog Main.o Similarity.o Scanner.o ScanLine.o Utils.o 
Undefined symbols for architecture x86_64:
  "Similarity::maxJaccard()", referenced from:
      _main in Main.o
  "Similarity::readData(Scanner&)", referenced from:
      _main in Main.o
  "Similarity::toString()", referenced from:
      _main in Main.o
  "Similarity::maxCosine()", referenced from:
      _main in Main.o
  "Similarity::Similarity()", referenced from:
      _main in Main.o
  "Similarity::~Similarity()", referenced from:
      _main in Main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [Aprog] Error 1

Thank you for reading through all that, any suggestions or solutions would be greatly appreciated.

感谢您阅读所有这些,任何建议或解决方案将不胜感激。

回答by john

There's no mystery here, the linker is telling you that you haven't defined the missing symbols, and you haven't.

这里没有什么神秘之处,链接器告诉你你没有定义丢失的符号,你也没有。

Similarity::Similarity()or Similarity::~Similarity()are just missing and you have defined the others incorrectly,

Similarity::Similarity()或者Similarity::~Similarity()只是缺少而您错误地定义了其他人,

void Similarity::readData(Scanner& inStream){
}

not

不是

void readData(Scanner& inStream){
}

etc. etc.

等等等等

The second one is a function called readData, only the first is the readData method of the Similarity class.

第二个是一个叫做 readData 的函数,只有第一个是 Similarity 类的 readData 方法。

To be clear about this, in Similarity.h

为了清楚这一点,在 Similarity.h 中

void readData(Scanner& inStream);

but in Similarity.cpp

但在 Similarity.cpp 中

void Similarity::readData(Scanner& inStream){
}