Generated moc names are not correct

2.1k Views Asked by At

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

There are 1 best solutions below

5
On

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