AUTOUIC
Should the target be processed with autouic (for Qt projects).
AUTOUIC is a boolean specifying whether CMake will handle the Qt uic code generator automatically, i.e. without having to use the QT4_WRAP_UI() or QT5_WRAP_UI() macro. Currently Qt4 and Qt5 are supported.
When this property is ON, CMake will scan the source files at build time and invoke uic accordingly. If an #include statement like #include “ui_foo.h” is found in foo.cpp, a foo.ui file is expected next to foo.cpp, and uic is run on the foo.ui file. This property is initialized by the value of the CMAKE_AUTOUIC variable if it is set when a target is created.
Additional command line options for uic can be set via the AUTOUIC_OPTIONS source file property on the foo.ui file. The global property AUTOGEN_TARGETS_FOLDER can be used to group the autouic targets together in an IDE, e.g. in MSVS.
qt中moc的作用
Qt 将源代码交给标准 C++ 编译器,如 gcc 之前,需要事先将这些扩展的语法去除掉。完成这一操作的就是 moc。
moc 全称是 Meta-Object Compiler,也就是“元对象编译器”。Qt 程序在交由标准编译器编译之前,先要使用 moc 分析 C++ 源文件。如果它发现在一个头文件中包含了宏 Q_OBJECT,则会生成另外一个 C++ 源文件。这个源文件中包含了 Q_OBJECT 宏的实现代码。这个新的文件名字将会是原文件名前面加上 moc_ 构成。这个新的文件同样将进入编译系统,最终被链接到二进制代码中去。因此我们可以知道,这个新的文件不是“替换”掉旧的文件,而是与原文件一起参与编译。另外,我们还可以看出一点,moc 的执行是在预处理器之前。因为预处理器执行之后,Q_OBJECT 宏就不存在了。
在命令行下输入
moc yourfilename.h -o moc_youfilename.cpp
生成不带Q_OBJENT的源文件。
总结起来就是:
-
moc 就是“元对象编译器”;
-
Qt程序在交给标准编译器预编译之前要使用 moc 分析 C++ 源文件;
-
如果有宏 Q_OBJECT,则生成一个包含Q_OBJECT 宏的实现代码的C++源文件;
-
新生成的源文件参与到标准编译器的编译中;
-
编译过程中如果找不到对应的moc文件就会出现链接错误,此时要添加上对应的moc文件;
AUTOMOC
Should the target be processed with automoc (for Qt projects).
AUTOMOC is a boolean specifying whether CMake will handle the Qt moc preprocessor automatically, i.e. without having to use the QT4_WRAP_CPP() or QT5_WRAP_CPP() macro. Currently Qt4 and Qt5 are supported.
When this property is set ON, 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, header files with the same base name (like foo.h) or _p appended to the base name (like foo_p.h) are parsed for Q_OBJECT macros, and if found, moc is also executed on those files. AUTOMOC checks multiple header alternative extensions, such as hpp, hxx etc when searching for headers. The resulting moc files, which are not included as shown above in any of the source files are included in a generated _automoc.cpp file, which is compiled as part of the target. This property is initialized by the value of the CMAKE_AUTOMOC variable if it is set when a target is created.
Additional command line options for moc can be set via the AUTOMOC_MOC_OPTIONS property.
By enabling the CMAKE_AUTOMOC_RELAXED_MODE variable the rules for searching the files which will be processed by moc can be relaxed. See the documentation for this variable for more details.
The global property AUTOGEN_TARGETS_FOLDER can be used to group the automoc targets together in an IDE, e.g. in MSVS.