“Symbol(s) not found for architecture x86_64” on Q

2019-01-06 20:20发布

问题:

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:

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.

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.

回答1:

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.

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.

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: -

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.



回答2:

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.

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.



回答3:

For me this problem resulted from not rebuilding the make file after adding another source file and header.

Under Build: Cleanall->run qMake->run

fixed the error for me.



回答4:

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.



回答5:

For me, I forgot to write the name of the class object when declaring the function in .cpp file.

wrong: int Zero(int &num)

right: int Common2::Zero(int &num)

Where Common2 is the class.