PDA

View Full Version : QT Unit Testing moc problem "unresolved external symbol" for QMetaObject



quimnuss
6th January 2017, 16:48
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:


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:


tst_someunittesttest.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl HelloWorld::metaObject(void)const " (?metaObject@HelloWorld@@UEBAPEBUQMetaObject@@XZ)

high_flyer
23rd January 2017, 23:51
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).

d_stranz
24th January 2017, 23:58
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:



class MyClass : public QObject // or some other class in the QObject hierarchy, like QWidget
{
Q_OBJECT; // << required

public:
MyClass( QObject * parent = 0 );

// ...
};

high_flyer
25th January 2017, 10:47
No, I don't think so. The .pro file is telling qmake to use "TEMPLATE = app".
Yes he is.
As I said:

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:


LIBS += -L"$$OUT_PWD/../HelloWorld/release"
LIBS += -lHelloWorld


It could be however that there are other problems as well.

d_stranz
25th January 2017, 15:50
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.

anda_skoa
26th January 2017, 12:28
On Windows symbols, such as classes, in a dynamic library, need to be exported using respective export/import macros.

Cheers,
_