- Qt 6.7
- Qt Core
- CMake Commands in Qt6 Core
- qt_wrap_cpp
- Qt 6.7.0 Reference Documentation

# qt_wrap_cpp

Creates .moc files from sources.

The command is defined in the `Core` component of the `Qt6` package, which can be loaded like so:

     find_package(Qt6 REQUIRED COMPONENTS Core)

This command was introduced in Qt 5.0.

## Synopsis

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

If versionless commands are disabled, use `qt6_wrap_cpp()` instead. It supports the same set of arguments as this command.

## Description

Creates rules for calling the Meta-Object Compiler (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>`.

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

## Arguments

You can set an explicit `TARGET`. This will make sure that the target properties `INCLUDE_DIRECTORIES` and `COMPILE_DEFINITIONS` are also used when scanning the source files with `moc`.

You can set additional `OPTIONS` that should be added to the `moc` calls. You can find possible options in the moc documentation.

The `OPTIONS` can evaluate generator expressions when `TARGET` is set.

Note: If the `OPTIONS` include both generator expressions and special characters, use variables to implement them. For example, use `$<ANGLE-R>`, `$<COMMA>` and `$<SEMICOLON>` instead of `>`, `,` 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` file using the Q_PLUGIN_METADATA() macro.

## Examples

     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
                 OPTIONS
                 "$<$<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)>")