Results 1 to 7 of 7

Thread: Problem when linking a Qt Unit Test with CMake

  1. #1
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Problem when linking a Qt Unit Test with CMake

    Hey @all,

    i'm trying to compile a simple QtTestLib app, which ends up in a "undefined reference to vtable..." error.

    Here is the simple source:
    Qt Code:
    1. #include <QtTest>
    2.  
    3. class TestDemo: public QObject {
    4. Q_OBJECT
    5.  
    6. private slots:
    7. void initTestCase();
    8. void cleanupTestCase();
    9. void firstTest();
    10.  
    11. };
    12.  
    13. void TestDemo::initTestCase() {
    14. }
    15.  
    16. void TestDemo::cleanupTestCase() {
    17. }
    18.  
    19. void TestDemo::firstTest() {
    20. QVERIFY(1 == 1);
    21. }
    22.  
    23. QTEST_MAIN(TestDemo)
    To copy to clipboard, switch view to plain text mode 

    And here is the corresponding CMakeLists.txt file:
    Qt Code:
    1. SET(QT_USE_QTMAIN TRUE)
    2. SET(QT_USE_QTTEST TRUE)
    3.  
    4. INCLUDE(${QT_USE_FILE})
    5. INCLUDE_DIRECTORIES(./)
    6.  
    7. SET(TESTDEMO_SOURCES
    8. main.cpp
    9. )
    10.  
    11. QT4_AUTOMOC(${TESTDEMO_SOURCES})
    12. ADD_EXECUTABLE(TestDemo ${TESTDEMO_SOURCES} ${TESTDEMO_MOC})
    13. TARGET_LINK_LIBRARIES(TestDemo ${QT_LIBRARIES})
    To copy to clipboard, switch view to plain text mode 

    The error I get is the following:
    Qt Code:
    1. -- Configuring done
    2. -- Generating done
    3. -- Build files have been written to: build/Debug
    4. Scanning dependencies of target TestDemo
    5. [100%] Building CXX object test/TestDemo/CMakeFiles/TestDemo.dir/main.cpp.obj
    6. Linking CXX executable ..\..\bin\TestDemo.exe
    7. CMakeFiles\TestDemo.dir\main.cpp.obj: In function `TestDemo':
    8. test/TestDemo/main.cpp:4: undefined reference to `vtable for TestDemo'
    9. CMakeFiles\TestDemo.dir\main.cpp.obj: In function `~TestDemo':
    10. test/TestDemo/main.cpp:4: undefined reference to `vtable for TestDemo'
    11. collect2: ld returned 1 exit status
    12. mingw32-make[3]: *** [bin/TestDemo.exe] Error 1
    13. mingw32-make[2]: *** [test/TestDemo/CMakeFiles/TestDemo.dir/all] Error 2
    14. mingw32-make[1]: *** [test/TestDemo/CMakeFiles/TestDemo.dir/rule] Error 2
    15. mingw32-make: *** [TestDemo] Error 2
    To copy to clipboard, switch view to plain text mode 

    Could anybody help me to fix the problem?

    Thanks in advance

    Best Regards
    NoRulez

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem when linking a Qt Unit Test with CMake

    You forgot to include your .moc file in the end
    Qt Code:
    1. QTEST_MAIN(TestDemo) // after this
    2. #include "testdemo.moc"
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Problem when linking a Qt Unit Test with CMake

    When I include "testdemo.moc" or "TestDemo.moc" i get the following error:
    Qt Code:
    1. test\TestDemo\main.cpp:34:24: error: testdemo.moc: No such file or directory
    2. test\TestDemo\main.cpp:34:24: error: TestDemo.moc: No such file or directory
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem when linking a Qt Unit Test with CMake

    Take a look in the project folder and see what is the name of .moc file

  5. #5
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Problem when linking a Qt Unit Test with CMake

    I think the problem is only when using MinGW, but anyway, how can I solve this problem without including the "*.moc" at the end of the file?

    Thanks in advance

  6. #6
    Join Date
    May 2010
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem when linking a Qt Unit Test with CMake

    http://qtnode.net/wiki/Qt4_with_cmake :

    If you don't use the #include "header.moc" convention, you can use the QT4_WRAP_CPP macro. This generates a list of moc_xxxx.cxx files to be generated. You pass in the list of headers to be moc'ed, and get back a list of source files to add to your build target. This is similar to how qmake works with Qt4

  7. #7
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: Problem when linking a Qt Unit Test with CMake

    The problem is, that I only have a "main.cpp" file, it doesn't use any header files.

Similar Threads

  1. How can we make unit test for multithread in QT
    By learning_qt in forum Qt Programming
    Replies: 8
    Last Post: 15th January 2013, 16:09
  2. Replies: 4
    Last Post: 7th May 2012, 15:39
  3. Unit test coverage
    By leoalvesmachado in forum Newbie
    Replies: 3
    Last Post: 16th April 2010, 13:48
  4. Unit Test Organisation, and having qmake what I want
    By jrharshath in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2009, 18:59
  5. How to unit test a Qt Gui
    By mitskits in forum Qt Programming
    Replies: 1
    Last Post: 20th January 2006, 07:36

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.