Results 1 to 6 of 6

Thread: QT Unit Testing moc problem "unresolved external symbol" for QMetaObject

  1. #1
    Join Date
    Jul 2015
    Posts
    52
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. QT += widgets network testlib
    2.  
    3. TARGET = tst_someunittesttest
    4. CONFIG += console
    5. CONFIG -= app_bundle
    6.  
    7. TEMPLATE = app
    8.  
    9. INCLUDEPATH += $$PWD/../HelloWorld
    10.  
    11. include($$PWD/../HelloWorld/helloworldCommon.pri)
    12.  
    13. LIBS += -L"$$OUT_PWD/../HelloWorld/release"
    14. LIBS += -lHelloWorld
    15.  
    16. message("Searching libs here $$LIBS")
    17.  
    18. SOURCES += tst_someunittesttest.cpp
    19. DEFINES += SRCDIR=\\\"$$PWD/\\\"
    To copy to clipboard, switch view to plain text mode 

    The first error's complete message:

    Qt Code:
    1. tst_someunittesttest.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl HelloWorld::metaObject(void)const " (?metaObject@HelloWorld@@UEBAPEBUQMetaObject@@XZ)
    To copy to clipboard, switch view to plain text mode 
    Last edited by quimnuss; 6th January 2017 at 16:53.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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).
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QT Unit Testing moc problem "unresolved external symbol" for QMetaObject

    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:

    Qt Code:
    1. class MyClass : public QObject // or some other class in the QObject hierarchy, like QWidget
    2. {
    3. Q_OBJECT; // << required
    4.  
    5. public:
    6. MyClass( QObject * parent = 0 );
    7.  
    8. // ...
    9. };
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QT Unit Testing moc problem "unresolved external symbol" for QMetaObject

    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:
    Qt Code:
    1. LIBS += -L"$$OUT_PWD/../HelloWorld/release"
    2. LIBS += -lHelloWorld
    To copy to clipboard, switch view to plain text mode 

    It could be however that there are other problems as well.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. The following user says thank you to high_flyer for this useful post:

    d_stranz (25th January 2017)

  6. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QT Unit Testing moc problem "unresolved external symbol" for QMetaObject

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

Similar Threads

  1. Replies: 13
    Last Post: 6th May 2019, 23:02
  2. Replies: 4
    Last Post: 9th May 2016, 12:30
  3. Replies: 1
    Last Post: 25th February 2016, 17:17
  4. Replies: 4
    Last Post: 5th January 2011, 15:51
  5. Unresolved external symbol "staticMetaObject"
    By sr71 in forum Qt Programming
    Replies: 5
    Last Post: 29th June 2010, 23:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.