Reputation: 35
I recently tried to write a unit test (with googletest) for a class including a Q_OBJECT macro and a self-defined signal. The test subproject won't compile (even after rebuilding/deleting everything) with the following linker errors:
"error: undefined reference to `vtable for Class'"
and
"error: undefined reference to `vtable for Class::signal()'"
My src subproject compiles just fine. After researching the issue I guess the problem is that the compiler doesn't generate moc files for the test subproject. Also I've not been successful to include the src subproject's moc files in test. How can I fix this?
Here are my .pro files:
Project .pro file
TEMPLATE = subdirs
CONFIG(debug, debug|release) {
SUBDIRS += \
src \
test
test.depends = src
} else {
SUBDIRS += \
src
QMAKE_CXXFLAGS += -O2
}
src.pro
QT += core gui charts widgets
TARGET = Project name
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000
HEADERS += \
...
SOURCES += \
...
test.pro
include(gtest_dependency.pri)
QT += core
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG += thread
CONFIG += qt
INCLUDEPATH += $$PWD/../src
DEPENDPATH += $$PWD/../src
HEADERS += \
... (only test headers)
SOURCES += \
... (test and src source files)
Upvotes: 2
Views: 450
Reputation: 20314
I had the same problem. Making sure Qt mocs the src headers also for the test project seem to require to mention them in the test projects .pro file too. Thus adding something like:
HEADERS += ( dependent src headers )
If the tests need all the src headers they could be extracted into a common .pri file.
Upvotes: 1