体系结构x86_64的未定义符号:并且找不到原因

我有我的main.cc,是

#include <iostream>
#include "Sally.h"
using namespace std;

int main(){
   Sally sallyObject;
   sallyObject.printCrap();

}

和头文件Sally.h,即

#ifndef SALLY_H
#define SALLY_H

class Sally{
public:
    Sally();
    void printCrap();
protected:
private:
};

#endif //BURRITO_H

和Sally.cc,即

#include "Sally.h"
#include <iostream>
using namespace std;

Sally::Sally()
{
}

void Sally::printCrap(){
    cout << "did someone say steak?" << endl;
}

,这三个文件位于同一目录中。 键入g++ main.cc时,无法构建代码。它说

Undefined symbols for architecture x86_64:
    "Sally::printCrap()",referenced from:
         _main in main-16cd07.o
    "Sally::Sally()",referenced from:
         _main in main-16cd07.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

有人知道为什么会这样吗?

a546335402 回答:体系结构x86_64的未定义符号:并且找不到原因

我认为您需要将所有.cc文件一起编译,因为不包含sally.cc。

g++ main.cc sally.cc

应该做到这一点。

包含dot-h文件使您的类的定义对所有人(包括main.cc)都可见,但是如果不对其进行编译,则看不到代码的实际主体。

本文链接:https://www.f2er.com/3126681.html

大家都在问