qtqmakeqobjectmoc

Generate moc file for header only class outside the project tree


I have some classes defined only using a headers file that inherits the QObject class and uses the Q_OBJECT macro. They are placed in a certain folder on my PC. I want to include these classes in my qt project.

I have written a .pri file (located in the same folder of these classes) in this way:

INCLUDEPATH += $$PWD
DEPENDPATH += $$PWD

In my .pro project file, I include the .pri (using include() command) and I can import the headers. The problem is that during the compilation I have some linkage errors related to QMetaObject, for example

Plc.obj:-1: error: LNK2001: external symbol "public: virtual struct QMetaObject const * __cdecl Conveyor::metaObject(void)const " (?metaObject@Conveyor@@UEBAPEBUQMetaObject@@XZ) not resolved

I think that the problem is related to the fact that qmake does not generate the moc file for each class, it only include the headers path. Infact if I remove the Q_OBJECT macro from these classes, I can compile successfully the project.

There is a way to tell at qmake, that it has to generate the moc file for these headers? I would like to avoid creating a library or a project for these classes. Or maybe a different solution?


Solution

  • I find the solution finally.

    I simply added the HEADER command at the end of the .pri file. Now the compiler scan the headers and create the moc file. ``

    My update .pri file is:

    INCLUDEPATH += $$PWD
    DEPENDPATH += $$PWD
    
    HEADERS += \
        $$PWD/MyClass1.h \
        $$PWD/MyClass2.h