PDA

View Full Version : Problem when linking a Qt Unit Test with CMake



NoRulez
29th May 2010, 12:16
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:


#include <QtTest>

class TestDemo: public QObject {
Q_OBJECT

private slots:
void initTestCase();
void cleanupTestCase();
void firstTest();

};

void TestDemo::initTestCase() {
}

void TestDemo::cleanupTestCase() {
}

void TestDemo::firstTest() {
QVERIFY(1 == 1);
}

QTEST_MAIN(TestDemo)


And here is the corresponding CMakeLists.txt file:


SET(QT_USE_QTMAIN TRUE)
SET(QT_USE_QTTEST TRUE)

INCLUDE(${QT_USE_FILE})
INCLUDE_DIRECTORIES(./)

SET(TESTDEMO_SOURCES
main.cpp
)

QT4_AUTOMOC(${TESTDEMO_SOURCES})
ADD_EXECUTABLE(TestDemo ${TESTDEMO_SOURCES} ${TESTDEMO_MOC})
TARGET_LINK_LIBRARIES(TestDemo ${QT_LIBRARIES})


The error I get is the following:


-- Configuring done
-- Generating done
-- Build files have been written to: build/Debug
Scanning dependencies of target TestDemo
[100%] Building CXX object test/TestDemo/CMakeFiles/TestDemo.dir/main.cpp.obj
Linking CXX executable ..\..\bin\TestDemo.exe
CMakeFiles\TestDemo.dir\main.cpp.obj: In function `TestDemo':
test/TestDemo/main.cpp:4: undefined reference to `vtable for TestDemo'
CMakeFiles\TestDemo.dir\main.cpp.obj: In function `~TestDemo':
test/TestDemo/main.cpp:4: undefined reference to `vtable for TestDemo'
collect2: ld returned 1 exit status
mingw32-make[3]: *** [bin/TestDemo.exe] Error 1
mingw32-make[2]: *** [test/TestDemo/CMakeFiles/TestDemo.dir/all] Error 2
mingw32-make[1]: *** [test/TestDemo/CMakeFiles/TestDemo.dir/rule] Error 2
mingw32-make: *** [TestDemo] Error 2


Could anybody help me to fix the problem?

Thanks in advance

Best Regards
NoRulez

Zlatomir
29th May 2010, 12:31
You forgot to include your .moc file in the end


QTEST_MAIN(TestDemo) // after this
#include "testdemo.moc"

NoRulez
29th May 2010, 13:43
When I include "testdemo.moc" or "TestDemo.moc" i get the following error:


test\TestDemo\main.cpp:34:24: error: testdemo.moc: No such file or directory
test\TestDemo\main.cpp:34:24: error: TestDemo.moc: No such file or directory

Zlatomir
29th May 2010, 13:49
Take a look in the project folder and see what is the name of .moc file

NoRulez
8th June 2010, 19:52
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

Onanymous
9th June 2010, 19:29
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

NoRulez
10th June 2010, 18:30
The problem is, that I only have a "main.cpp" file, it doesn't use any header files.