【发布时间】:2020-10-15 16:04:50
【问题描述】:
您好,我尝试使用 xcode 从子目录导入 c++ 类,但收到错误消息:
Undefined symbols for architecture x86_64:
"Mother::Mother()", referenced from:
_main in main.o
"Mother::~Mother()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我不明白,因为代码在终端上使用“clang++”与这个架构一起工作,可能是我错过了导入文件的内容,或者可能是在设置中要做的事情? 在屏幕截图中,我们看到所有文件都已导入。有关信息,模板文件或带有子目录的函数没有问题,只有将类放在子目录中,当类文件位于根目录时也可以。我希望这很清楚,如果任何机构对我有解决方案或帮助,那就太好了。
祝你有美好的一天。
我的配置: OSX 10.14.8 / Xcode 11.3.1
这里有一个简单的代码来重现:
main.c
#include <iostream>
#include "other/Mother.hpp"
int main(int argc, const char * argv[]) {
Mother Mother;
return 0;
}
Mother.hpp
#ifndef MOTHER_H
# define MOTHER_H
#include <iostream>
#include <string>
class Mother {
public:
Mother();
~Mother();
};
#endif
mother.cpp
#include "./Mother.hpp"
Mother::Mother() {
return;
}
Mother::~Mother() {
return;
}
【问题讨论】:
-
我不知道 XCode,但我假设有一些“项目”文件列出了所有要编译的文件。确保
Mother.cpp与main.cpp一起列出。 -
@Yksisarvinen 是的,我添加了文件,我使用模板和函数进行了测试,这很有效。这只发生在课堂上。当这个在子目录中时。
标签: c++ xcode include undefined-reference