C++ QtCreator 项目上的“找不到架构 x86_64 的符号”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18973042/
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
"Symbol(s) not found for architecture x86_64" on QtCreator project
提问by MaiaVictor
I'm getting the error
我收到错误
Symbol(s) not found for architecture x86_64
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:
试图在 QtCreator 上编译一个项目。当我尝试创建用户定义类的实例时会发生这种情况,Layer
. 该类由标题layer.h
和实现 组成layer.cpp
。它已经过测试并在另一个程序中工作。在我的项目中,它包含在中qtwidget.h
,当我尝试在qtwidget.cpp
. 例如:
Layer<double> text("pq.txt",0.5,0.5,0.5);
Having this line on qtwidget.cpp
is enough for the error to show up.
打开这条线就qtwidget.cpp
足以显示错误了。
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.
这是一个如此普遍的错误,我对如何进一步隔离它一无所知,但如果有帮助,我已将整个项目包含在此 git repo 中。
回答by TheDarkKnight
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:
在我看来,Qt Creator 显示的错误消息在您理解之前非常具有误导性,但并不能阻止将模板类拆分为头文件和实现文件。如果您考虑以下消息:
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 Issuesoutput 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.
当我看到这个时,我最初认为这个问题是它在问题输出中自己说明了这个错误,并可能导致用户认为问题是由架构引起的。实际上,它的全部意思是有一个已定义的符号(通常是函数),其匹配的实现未找到。
If you change from Issuesto the Compile Outputwindow 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 Issuesview.
如果您从Issues更改为Compile Output窗口并向上滚动,您将能够准确地看到哪些符号无法找到;我的显示为红色。令人讨厌的是缺少符号的详细信息没有显示在问题视图中。
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
只需将函数定义添加到标头中,无需实现该函数,只需从 .cpp 文件中调用它,即可轻松复制此错误。然后,您将在“问题”窗口中看到类似的内容
Switching to the Compile Output view and scrolling up displays this: -
切换到编译输出视图并向上滚动显示:-
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.
所以现在我们看到实际的问题是调用 PGGui 的类中的函数 DoSomeStuff 是从构造函数 PGGui::PGGui 调用的,但是缺少 DoSomeStuff 的主体,因为没有找到它的符号。
回答by MaiaVictor
Fortunately I've managed to solve my problem before any answers, so, if anyone is experiencing anything similar, the issue was that it seems you can't split a templated class into a .cpp and a .h file. Putting all declarations of the .cpp
file back into the .h
solved the issue.
幸运的是,我在得到任何答案之前设法解决了我的问题,因此,如果有人遇到类似的问题,问题是您似乎无法将模板化类拆分为 .cpp 和 .h 文件。将.cpp
文件的所有声明放回.h
解决了问题。
I still had a leftover problem, though: duplicated symbols (which was the reason I split it). This time, declaring a variable as external
in the .h
, and redeclaring it without the external
keyword in one (and only one) .cpp
file solved the issue for good.
不过,我仍然有一个遗留问题:重复的符号(这就是我拆分它的原因)。这一次,变量声明为external
在.h
和重新声明它没有external
一个(也是唯一一个)关键字.cpp
文件解决问题为好。
回答by MaiaVictor
For me this problem resulted from not rebuilding the make file after adding another source file and header.
对我来说,这个问题是由于在添加另一个源文件和头文件后没有重建 make 文件造成的。
Under Build: Cleanall->run qMake->run
在 Build 下: Cleanall->run qMake->run
fixed the error for me.
为我修复了错误。
回答by Matthias
The problem in my case was that i had a project with many subproject and one of the pro files of the subprojects was empty.
我的问题是我有一个包含许多子项目的项目,并且子项目的 pro 文件之一是空的。
回答by Cheng
For me, I forgot to write the name of the class object when declaring the function in .cpp file.
对我来说,在 .cpp 文件中声明函数时忘记写类对象的名称。
wrong: int Zero(int &num)
错误的: int Zero(int &num)
right: int Common2::Zero(int &num)
对: int Common2::Zero(int &num)
Where Common2 is the class.
其中 Common2 是类。