本页内容

qt_generate_moc

对输入文件调用 moc 命令。

该命令定义在Qt6 包的Core 组件中,可按以下方式加载:

find_package(Qt6 REQUIRED COMPONENTS Core)

该命令于 Qt 5.0 中引入。

语法

qt_generate_moc(src_file dest_file
                [TARGET target])

如果禁用了无版本号的命令,请改用 `qt6_generate_moc() `。它支持与本命令相同的参数集。

描述

创建一条规则,用于对src_file 调用Meta-Object Compiler (moc),并将输出存储在dest_file 中。

注意:这是一个 低级宏。有关让源文件通过moc 进行处理的更便捷方法,请参阅CMake AUTOMOC 文档。qt_wrap_cpp()功能类似,但会自动为您生成临时文件路径。

参数

您可以显式设置TARGET 。这将确保在使用moc 扫描源文件时,目标属性INCLUDE_DIRECTORIES 和COMPILE_DEFINITIONS 也会被采用。

示例

qt_generate_moc(main.cpp main.moc TARGET myapp)

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.