qt_wrap_cpp

從源創建 .moc 文件。

命令的定義在 Core 組件對於 Qt6 包,可以像這樣加載:

find_package(Qt6 REQUIRED COMPONENTS Core)
					

該命令在 Qt 5.0 引入。

概要

qt_wrap_cpp supports two signatures. The first signature was added in Qt 6.8 and has the following form:

qt_wrap_cpp(<TARGET> src_file1 [src_file2 ...]
            [OPTIONS ...]
            [DEPENDS ...])
					

注意: The signature above is recommended over the older signature.

qt_wrap_cpp(<VAR> src_file1 [src_file2 ...]
            [TARGET target]
            [OPTIONS ...]
            [DEPENDS ...])
					

無版本命令 被禁用,使用 qt6_wrap_cpp() 代替。它支持如此命令的一組相同自變量。

描述

Creates rules for calling the MOC (元對象編譯器) on the given source files. For each input file, an output file is generated in the build directory. The paths of the generated files are added to <VAR> .

注意: This is a low-level macro. See the CMake AUTOMOC Documentation for a more convenient way to let source files be processed with moc .

自變量

可以明確設置 TARGET . This will make sure that the target properties INCLUDE_DIRECTORIES and COMPILE_DEFINITIONS are also used when scanning the source files with moc .

Since Qt 6.8, when a source file is passed to qt_wrap_cpp instead of a header file to generate a .moc file for a target, the TARGET parameter is needed to set the correct include path for the generated .moc file in the source file. As generated .moc files are added to the target's sources by qt_wrap_cpp , they are not added to <VAR> .

可以設置額外 OPTIONS 應該被添加到 moc 調用。可以查找可能的選項在 moc documentation .

The OPTIONS can evaluate generator expressions when TARGET 有設置。

注意: OPTIONS include both generator expressions and special characters, use variables to implement them. For example, use $<ANGLE-R> , $<COMMA> and $<SEMICOLON> 而不是 > , , and : . Otherwise, the generator expression will not be evaluated correctly. OPTIONS are wrapped in generator expressions, so you must escape special characters in them.

DEPENDS allows you to add additional dependencies for recreation of the generated files. This is useful when the sources have implicit dependencies, like code for a Qt plugin that includes a .json 文件使用 Q_PLUGIN_METADATA () 宏。

範例

Since Qt 6.8:

qt_add_executable(myapp myapp.cpp main.cpp)
qt_wrap_cpp(myapp myapp.cpp)
					


// myapp.cpp
#include "myapp.h"
#include <QObject>
class MyApp : public QObject {
    Q_OBJECT
public:
    MyApp() = default;
};
#include "myapp.moc"
					

In the above file, myapp.moc is included in myapp.cpp . To generate the myapp.moc file, the qt_wrap_cpp macro is used with the TARGET parameter. The .moc file and its path will be added to the target's sources and include directories by the qt_wrap_cpp 宏。

The old version:

set(SOURCES myapp.cpp main.cpp)
qt_wrap_cpp(SOURCES myapp.h)
qt_add_executable(myapp ${SOURCES})
					

In the following example, the generator expressions passed to OPTIONS will be evaluated since TARGET is set. The argument is specified this way to avoid syntax errors in the generator expressions.

set(SOURCES myapp.cpp main.cpp)
qt_wrap_cpp(SOURCES myapp.h
            TARGET myapp
            選項
            "$<$<CONFIG:Debug>:-DMY_OPTION_FOR_DEBUG>"
            "-DDEFINE_CMDLINE_SIGNAL=void cmdlineSignal(const QMap<int, int> &i)"
            "$<$<CONFIG:Debug>:-DDEFINE_CMDLINE_SIGNAL_IN_GENEX=void cmdlineSignal(const QMap<int$<COMMA> int$<ANGLE-R> &i)>")
qt_add_executable(myapp ${SOURCES})
					

The following example uses target_compile_definitions to set COMPILE_DEFINITIONS which will be added to OPTIONS .

set(SOURCES myapp.cpp main.cpp)
qt_wrap_cpp(SOURCES myapp.h
            TARGET myapp)
qt_add_executable(myapp ${SOURCES})
target_compile_definitions(myapp PRIVATE "$<$<CONFIG:Debug>:MY_OPTION_FOR_DEBUG>"
                                         "DEFINE_CMDLINE_SIGNAL=void cmdlineSignal(const QMap<int, int> &i)"
                                         "$<$<BOOL:TRUE>:DEFINE_CMDLINE_SIGNAL_IN_GENEX=void cmdlineSignal(const QMap<int$<COMMA> int$<ANGLE-R> &i)>")