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:
Code:
#include <QtTest>
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:
Code:
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:
Code:
-- 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
Re: Problem when linking a Qt Unit Test with CMake
You forgot to include your .moc file in the end
Code:
QTEST_MAIN(TestDemo) // after this
#include "testdemo.moc"
Re: Problem when linking a Qt Unit Test with CMake
When I include "testdemo.moc" or "TestDemo.moc" i get the following error:
Code:
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
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
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
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
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.