Errors in generated MOC files for QT5 from cmake

断了今生、忘了曾经 提交于 2019-12-03 08:56:47

CMake documentation is not that bad, do not neglect reading it. You misunderstood the concept of AUTOMOC:

AUTOMOC is a boolean specifying whether CMake will handle the Qt moc preprocessor automatically, i.e. without having to use the QT4_WRAP_CPP() macro. Currently Qt4 is supported. When this property is set to TRUE, CMake will scan the source files at build time and invoke moc accordingly. If an #include statement like #include "moc_foo.cpp" is found, the Q_OBJECT class declaration is expected in the header, and moc is run on the header file. If an #include statement like #include "foo.moc" is found, then a Q_OBJECT is expected in the current source file and moc is run on the file itself. Additionally, all header files are parsed for Q_OBJECT macros, and if found, moc is also executed on those files.

So, first of all, you should not add generated moc files explicitly to sources and push them into executable compilation. In other words, you only push your sources:

SET(SRC
  src/main.cpp
  src/video_widget_surface.cpp
  src/video_widget.cpp
  src/video_player.cpp)

and moc ones are handled automatically by CMake.

Secondly, as stated in the documentation:

  • If Q_OBJECT is in the foo.h (i.e. QObject is declared in the header file), then in the corresponding foo.cpp don't forget to add #include "moc_foo.cpp", preferably at the end of the file;

  • If Q_OBJECT is in the foo.cpp (i.e. QObject is declared in the source file), then, again, in the foo.cpp itself don't forget to add #include "foo.moc", preferably at the end of the file.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!