2

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.

Wazery
  • 15,394
  • 19
  • 63
  • 95
  • 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 Jul 26 '12 at 21:34
  • 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 Jul 26 '12 at 21:37

1 Answers1

2

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

Kamil Klimek
  • 12,884
  • 2
  • 43
  • 58
  • Thanks for your answer, but the compiler gives me "undefined reference to " my constructor and the functions it this class. It compiles fine without the Q_OBJECT macro. What do you think. – Wazery Jul 27 '12 at 17:03
  • Also I want to say that in my project there is another class that has similar implementation and it compiles fine and generates a .moc. I don't know why this class generate moc_*.cpp instead. – Wazery Jul 27 '12 at 17:26
  • sorry, I don't know the reason of that behaviour. I remember that I've used Qt with cmake without any problem. You're doing something wrong here. Do you have Qt cmake modules? – Kamil Klimek Jul 27 '12 at 19:24
  • The rest of the project classes are behaving just fine, my problem is that why this class that is very similar to the other fine classes not generating a .moc file. – Wazery Jul 27 '12 at 21:12
  • because as I said it should (and it does) generate moc_*.cpp that should be automaticly added as source file – Kamil Klimek Jul 27 '12 at 23:27