3
votes

I am working on a Qt based project that uses cmake. All of my generated moc files are named *.moc, but I have some files that their generated moc files have names moc_*.cpp, not *.moc. Why this happens and how I can fix these files.

EDIT:

  • I want to say that these classes are inheriting from QObject and has the Q_OBJECT and Q_DECLARE_PUBLIC macros, and they don't compile with me unless there is a .moc for them.

  • I must include the .moc files in my .cpp files.

- The thing that makes me go crazy that I have an identical class (identical implementation to my class) that generates a .moc but my class generate moc_*.cpp.

1
Your files belonging to those moc files which are named *.moc, do they consist of both a *.h and a *.cpp? The *.moc files (not the moc_*.cpp files), are they put in the build or the source directory?leemes
Just for explanation in my cmakelist files I add the .cpp file which make both .h and .cpp compiled well and in my .cpp I include the .moc file only with no .h, the moc_*.cpp are generated in the same build directory that .moc are generated.Wazery

1 Answers

2
votes

You don't have to include *.moc file in every case of Q_OBJECT use. .moc files are generated only for classes that are declared in .cpp files. In other cases moc generates moc_*.cpp that includes your Q_OBJECT based class on it's own. You don't have anything to be worried about. Remove *.moc includes from your cpp files. for example:

main.cpp

class E: public QObject
{
    Q_OBJECT
};

moc will generate main.moc file to be included in main.cpp

Another example

class.h

class E: public QObject
{
     Q_OBJECT
public:
     void member();
};

class.cpp

#include "class.h"

void E::member()
{
}

moc will generate moc_class.cpp that includes class.h and is separate compilation unit