Moc not generating functions
-
I have following class
.h#define CONFIGURATION_H #include <QObject> #include <QtCore> class Configuration : public QObject { Q_OBJECT public: explicit Configuration(QObject* parent = nullptr); ~Configuration(); void init(); protected: private: public slots: private slots: }; #endif CONFIGURATION_H
.cpp
#include "Configuration.h" Configuration::Configuration(QObject* parent) : QObject(parent) {} Configuration::~Configuration() {} void Configuration::init() {} #include "moc_Configuration.cpp"
When using
Configuration
frommain.cpp
it's working.
When usingConfiguration
from the TestSuit (Qt Test) I get:- C:\Dev\Build\ProjectPewPew\Release\6.5.2\x64\src\service\test\tst_Configuration.obj:-1: error: LNK2019: Verweis auf nicht aufgel”stes externes Symbol ""public: __cdecl Configuration::Configuration(class QObject *)" (??0Configuration@@QEAA@PEAVQObject@@@Z)" in Funktion ""private: static void __cdecl TestConfig::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@TestConfig@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z)".
- C:\Dev\Build\ProjectPewPew\Release\6.5.2\x64\src\service\test\tst_Configuration.obj:-1: error: LNK2019: Verweis auf nicht aufgel”stes externes Symbol ""public: virtual __cdecl Configuration::~Configuration(void)" (??1Configuration@@UEAA@XZ)" in Funktion ""private: static void __cdecl TestConfig::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@TestConfig@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z)".
- C:\Dev\Build\ProjectPewPew\Release\6.5.2\x64\src\service\test\tst_Configuration.obj:-1: error: LNK2019: Verweis auf nicht aufgel”stes externes Symbol ""public: void __cdecl Configuration::init(void)" (?init@Configuration@@QEAAXXZ)" in Funktion ""private: static void __cdecl TestConfig::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@TestConfig@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z)".
I'm using the class as followed in
main
#include <QCoreApplication> #include <QtCore> #include "Configuration/Configuration.h" int main(int argc, char* argv[]) { // Instancing the application QCoreApplication a(argc, argv); Configuration config; config.init(); // Getting the result of the application int result(a.exec()); // Returning the result return result; }
in the TestSuit as followed:
#include <QObject> #include <QTest> #include <QtCore> #include "../../../app/src/Configuration/Configuration.h" class TestConfig : public QObject { Q_OBJECT public: private slots: void initTestCase(); void init(); void cleanup(); void cleanupTestCase(); // Allows access to private members without accessors friend class Configuration; }; void TestConfig::initTestCase() { Configuration cfg; } void TestConfig::init() {} void TestConfig::cleanup() {} void TestConfig::cleanupTestCase() {} QTEST_APPLESS_MAIN(TestConfig) #include "tst_Configuration.moc"
I only have a vague idea of what is happening. Can anyone enlighten me on why it's not working and how to fix this problem?
-
@Redman said in Moc not generating functions:
#define CONFIGURATION_H
don't you miss a '#ifndef CONFIGURATION_H' here?
Please show your CMakeLists.txt - I would guess you don't link against the lib where configuration.cpp is compiled with.
-
@Christian-Ehrlicher
#ifndef CONFIGURATION_H is present. I missed it while copying.My project structure:
QT += testlib QT -= gui TARGET = TestService CONFIG += qt console warn_on depend_includepath testcase CONFIG -= app_bundle CONFIG -= debug_and_release TEMPLATE = app SOURCES += \ $$files("$$PWD/src/*.cpp", true)
-
You don't compile configuration.cpp anywhere in your pro-file so ... or is it in testcase?
-
@Christian-Ehrlicher
Configuration.cpp is in service->app->src->Configuration
Its compiled correct and can be used from the main.cpp which is in service->app->srcQT += \ core TARGET = service TEMPLATE = app CONFIG += c++17 # Stop make from creating these folders in the build folder CONFIG -= debug_and_release MOC_DIR = $$OUT_PWD/tmp OBJECTS_DIR = $$OUT_PWD/tmp UI_DIR = $$OUT_PWD/tmp RCC_DIR = $$OUT_PWD/tmp SOURCES += \ $$files("$$PWD/*.cpp",true) \ HEADERS += \ $$files("$$PWD/src/*.h",true)
-
@Redman said in Moc not generating functions:
SOURCES +=
$$files("$$PWD/*.cpp",true) \Apart from the really strange way to add sources - this does not add configuration.cpp.
As you can see in the linker error not the moc stuff is missing but the functions defined in configuration.cpp
-
@Christian-Ehrlicher
Any tips on how to add sources without typing every file specifically. I assumed as long as the files showed up in the project tree building them is no problem. Which was true until now. -
@Redman Type all. But that's not the problem here.
-
@Christian-Ehrlicher So how can I tell moc to add the functions?
-
@Redman said in Moc not generating functions:
@Christian-Ehrlicher So how can I tell moc to add the functions?
As I already said there is no problem with moc but you don't compile configuration.cpp anywhere.
-
@Christian-Ehrlicher
The service.exe, which is configured in app.pro, is built correct. The exe can only be a result of a successful compilation. So I'd assume configuration.cpp is compiled -
@Redman said in Moc not generating functions:
So I'd assume configuration.cpp is compiled
Don't assume, check.
You did not show any pro file where you compile configuration.cpp so my answer still stands... -
I think this should answer your question.
To elaborate further:
project.proSUBDIRS = \ client \ logger \ service # where to find the sub projects - give the folders client.subdir = src/client logger.subdir = src/logger service.subdir = src/service
TEMPLATE = subdirs SUBDIRS = \ app \ test \ # where to find the sub projects - give the folders app.subdir = app test.subdir = test test.depends = app
So far all I had to do was include the sources and headers in the pro files and the projects simply compiled. Please enlighten me if I'm missing something crucial.
update:
Afterdumpbin.exe Configuration.obj /disasm
Even though we see the function is present in the obj file I still get following error
error: LNK2019: unresolved external symbol ""public: __cdecl Configuration::Configuration(class QObject *)" (??0Configuration@@QEAA@PEAVQObject@@@Z)" in function ""private: static void __cdecl TestConfig::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@TestConfig@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z)".
What makes me wonder is the second part of the error message. But I guess thats just how Qt invokes all function calls for classes that inherit from QObject.
-
Provide a minimal, compilable example. Don't use global globs to add files so we can see where exactly configuration.cpp is compiled into since you still (after 5 posts from me) failed to do so.
-
-
As I said - there is no library where you link your configuration.cpp - you only compile it directly to your app.
So either also directly link it to your testcase or create a library and link the library to your app and your test. -
@Redman said in Moc not generating functions:
SOURCES +=
$$files("$$PWD/src/*.cpp", true)If you insist on using this any further, you need to adjust your paths. To add configuration.cpp to your test.pro the path needs to be $$PWD/../app/src/*.cpp because $$PWD is service/test, but your sources are locate in service/app/src.
$$PWD
stands for "print working directory" and thus your working directory for app.pro and test.pro is different. -