“找不到架构 x86_64 的符号";在 QtCreator 项目中
我收到错误
Symbol(s) not found for architecture x86_64
尝试在 QtCreator 上编译项目.当我尝试创建用户定义的类 Layer
的实例时会发生这种情况.该类由标题 layer.h
和实现 layer.cpp
组成.它已经过测试并在另一个程序中工作.在我的项目中,它包含在 qtwidget.h
中,当我尝试在 qtwidget.cpp
上使用它时会发生错误.例如:
Trying to compile a project on QtCreator. It happens when I try to create an instance of an user defined class, Layer
. That class consists of a header, layer.h
, and a implementation, layer.cpp
. It was tested and works in another programs. On my project, it is included in qtwidget.h
and the error happens when I try to use it on qtwidget.cpp
. For example:
Layer<double> text("pq.txt",0.5,0.5,0.5);
在 qtwidget.cpp
上添加这一行足以显示错误.
Having this line on qtwidget.cpp
is enough for the error to show up.
这是一个非常普遍的错误,我对如何进一步隔离它一无所知,但如果有帮助,我已将整个项目包含在 这个git repo.
This is such a generic error that I'm clueless on how to isolate it any further, but if it helps, I've included the whole project on this git repo.
推荐答案
在我看来,Qt Creator 显示的错误信息在你理解之前是相当具有误导性的,但并不能阻止将模板类拆分为头文件和实现文件.如果您考虑一下消息:
In my opinion, the error message that Qt Creator displays is quite misleading until you understand it, but does not prevent splitting the template class into a header and implementation file. If you think about the message:
Symbol(s) not found for architecture x86_64
问题,当我看到这个时,我最初认为,是它在问题输出中自己说明了这个错误,并可能导致用户认为问题是由架构引起的.实际上,它的全部意思是有一个已定义的符号(通常是函数),但找不到匹配的实现.
the problem, I originally thought when I saw this, is that it states this error on its own in the Issues output and can lead the user into thinking that the problem is due to the architecture. Actually, all its saying is that there's a defined symbol (often function) whose matching implementation wasn't found.
如果您从 Issues 切换到 Compile Output 窗口并向上滚动,您将能够准确地看到哪些符号找不到;我的显示为红色.令人讨厌的是,问题 视图中没有显示缺失符号的详细信息.
If you change from Issues to the Compile Output window and scroll up, you'll be able to see exactly what symbols can't be found; mine's displayed in red. It's just annoying that the detail of the missing symbol(s) doesn't show up in the Issues view.
只需将函数定义添加到标头中即可轻松复制此错误,而无需实现该函数,而是从 .cpp 文件中调用它.然后,您将在问题"窗口中看到类似的内容
It's easy to replicate this error by just adding a function definition into a header and without implementing the function, call it from the .cpp file. You'll then see something like this in the Issues window
切换到编译输出视图并向上滚动显示:-
Switching to the Compile Output view and scrolling up displays this: -
所以现在我们看到实际的问题是调用 PGGui 的类中的函数 DoSomeStuff 是从构造函数 PGGui::PGGui 中调用的,但是缺少 DoSomeStuff 的主体,因为没有找到它的符号.
So now we see that tthe actual problem is that the function DoSomeStuff in the class called PGGui is being called from the constructor PGGui::PGGui, but the body of DoSomeStuff is missing, as its symbol is not found.
相关文章