QT Unit Testing moc problem "unresolved external symbol" for QMetaObject
I'm trying to add unit tests to a project of mine for the first time.
I can run mock tests alright (without using my project's classes) and run the application alright. But if I instantiate objects from the project I get an unresolved external symbol of the QMetaObject. If I recall correctly, this means the moc of the object isn't being included on the project.
How do I fix this? I have the same issue using googletests. The guide also doesn't help on this. I've tried installing the qt unit testing plugin, same result.
I've uploaded a mock project that follows the same structure that I'm using in the aforementioned project, fetch it here: https://github.com/quimnuss/QtUnitTestingTest
I'm using a static build of qt on windows, but I guess that's irrellevant. Using QtCreator as IDE and NMAke build.
I've also tried add the HelloWorld.lib, but taking a look at the Makefile.release it isn't used.
Somebody has an idea of what I'm doing wrong?
Here's the unit testing .pro:
Code:
QT += widgets network testlib
TARGET = tst_someunittesttest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
INCLUDEPATH += $$PWD/../HelloWorld
include($$PWD/../HelloWorld/helloworldCommon.pri)
LIBS += -L"$$OUT_PWD/../HelloWorld/release"
LIBS += -lHelloWorld
message("Searching libs here $$LIBS")
SOURCES += tst_someunittesttest.cpp
DEFINES += SRCDIR=\\\"$$PWD/\\\"
The first error's complete message:
Code:
tst_someunittesttest.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl HelloWorld::metaObject(void)const " (?metaObject@HelloWorld@@UEBAPEBUQMetaObject@@XZ)
Re: QT Unit Testing moc problem "unresolved external symbol" for QMetaObject
I had only a very brief look, so probably there are other problems too, but one problem is for sure:
Your helloworld is not a lib, but an app - yet you are telling your QtTest that it is lib and you are trying to link to it as such, which of course can't work.
Either bundle your classes in to a lib and link to it from your application as well as from your test, or, you could have your test application compile the classes of your hellowworld (basically giving a test main instead of the application main function).
Re: QT Unit Testing moc problem "unresolved external symbol" for QMetaObject
Quote:
yet you are telling your QtTest that it is lib
No, I don't think so. The .pro file is telling qmake to use "TEMPLATE = app".
The problem the OP is seeing is usually because a class has been derived from QObject, but the Q_OBJECT macro has not been placed in the class definition:
Code:
class MyClass
: public QObject // or some other class in the QObject hierarchy, like QWidget{
Q_OBJECT; // << required
public:
// ...
};
Re: QT Unit Testing moc problem "unresolved external symbol" for QMetaObject
Quote:
No, I don't think so. The .pro file is telling qmake to use "TEMPLATE = app".
Yes he is.
As I said:
Quote:
helloworld is not a lib, but an app
Which is what the pro file for HelloWorld is saying, as TEMPLATE=app says.
But to the test you are telling HellowWorld is a lib:
Code:
LIBS += -L"$$OUT_PWD/../HelloWorld/release"
LIBS += -lHelloWorld
It could be however that there are other problems as well.
Re: QT Unit Testing moc problem "unresolved external symbol" for QMetaObject
Quote:
But to the test you are telling HellowWorld is a lib
Yes, looks that way - I missed that line. It's a wonder that qmake can make any sense of this file.
Re: QT Unit Testing moc problem "unresolved external symbol" for QMetaObject
On Windows symbols, such as classes, in a dynamic library, need to be exported using respective export/import macros.
Cheers,
_