問題描述
我收到錯誤
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.
這篇關于“找不到架構 x86_64 的符號";在 QtCreator 項目中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!