mercredi 29 juin 2016

Cannot compile MOC file if Q_OBJECT macro is removed by other macro: The header file doesn't include <QObject>

When compiling a non-Qt version of a Qt/C++ program (-DDISABLE_MYMODULE), I get this compiler error:

src/mymodule.moc.cpp:12:2: error: #error "The header file
'mymodule.hpp' doesn't include <QObject>."
src/mymodule.moc.cpp:19:1: error: ‘QT_BEGIN_MOC_NAMESPACE’ does not name a type
src/mymodule.moc.cpp:40:6: error: ‘MyModule’ has not been declared
...

I'm using GNU make (not qmake). My Makefile compiles two object files per module, one directly from the .cpp source file and the MOC object file from the .moc.cpp source file (this is what doesn't work), both using g++. This .moc.cpp source file is created by MOC, from the .hpp header (this process does not throw an error).

The header file in question looks somewhat like this:

#ifndef DISABLE_MYMODULE //My problem macro
#ifndef MYMODULE_HPP
#define MYMODULE_HPP
//...
class MyModule : //...
{
    Q_OBJECT //Qt problem macro
    //...
};
//...
#endif
#endif

The whole thing will compile (and later link, execute) just fine if don't set my problem macro. If I do set it, but comment out QT's problem macro, it'll compile fine as well (building a non-Qt version).

I don't exactly know, what MOC replaces Q_OBJECT by, but shouldn't it still be inside my DISABLE_MYMODULE and therefore be removed by the preprocessor?

Aucun commentaire:

Enregistrer un commentaire