qt_add_grpc | Qt GRPC 6.6.2
Description: Generates Qt-based C++ services using a protobuf schema

- Qt 6.6
- Qt GRPC
- CMake Commands in Qt6 GRPC
- qt_add_grpc
- Qt 6.6.2 Reference Documentation

# qt_add_grpc

Generates Qt-based C++ services using a protobuf schema

Note: This command is in technology preview and may change in future releases.

This command was introduced in Qt 6.5.

Usually `qtgrpcgen` is invoked through the `qt_add_grpc` CMake macro.

     qt_add_grpc(<target> <TARGET_TYPE>
         PROTO_FILES <file> ...
         [COPY_COMMENTS]
         [GENERATE_PACKAGE_SUBFOLDERS]
         [EXTRA_NAMESPACE <namespace>]
         [EXPORT_MACRO <infix>]
         [PROTO_INCLUDES <path> ...]
         [OUTPUT_DIRECTORY <dir>]
         [OUTPUT_HEADERS <var>]
         [OUTPUT_TARGETS <var>]
     )

The source files are created by `qtgrpcgen` and added to the target. If the target already exists, the files are added to the target source list. If the target doesn't exist, it is created as a library which you must link to.

The gRPC library that is generated using the `qt_add_grpc` command refers to the protobuf symbols that are used in service calls. This means you either need to use a common target in both qt_add_protobuf and qt_add_grpc calls or link the generated protobuf library to the gRPC one.

Considering the above statement you may reuse the target for both protobuf and gRPC client code:

     # Generate the protobuf types first
     qt_add_protobuf(test_service_client
         PROTO_FILES
             test_service.protobuf
     )
     ...
     # Reuse the protobuf target and extend it with gRPC client
     # functionality
     qt_add_grpc(test_service_client CLIENT
         PROTO_FILES
             test_service.protobuf
     )

If you want to have separate targets then you need to link the gRPC client target to the protobuf one:

     # Generate the protobuf types first
     qt_add_protobuf(test_service_protobuf
         PROTO_FILES
             test_service.protobuf
     )
     ...
     # Add separate target with the generated code of the gRPC client.
     qt_add_grpc(test_service_client CLIENT
         PROTO_FILES
             test_service.protobuf
     )
     target_link_libraries(test_service_client PRIVATE test_service_protobuf)

## Arguments

- `TARGET_TYPE` is the type of gRPC code to be generated. Since gRPC supports a client-server architecture, `CLIENT` or `SERVER` source code may be generated.

  Note: for Qt 6.5 only `CLIENT` source code generation is supported.

- `COPY_COMMENTS` copies comments from `.proto` files. If provided in the parameter list, comments related to messages and fields are copied to generated header files.
- `GENERATE_PACKAGE_SUBFOLDERS` generates a folder structure for the generated files matching the `.proto` file's package name. For example, `package io.qt.test;` would put the generated files into `io/qt/test/`.
- `EXTRA_NAMESPACE` is an optional namespace that will be used for the generated classes. The classes are always generated in a namespace whose name is the same as the package name specified in the `.proto` file. If this option is used, then everything will be nested inside the extra namespace.
- `EXPORT_MACRO` is the base name of the symbol export macro used for the generated code. The generated macro name is constructed as `QPB_<EXPORT_MACRO>_EXPORT`. If the option is not set, the macro is not generated.
- `PROTO_FILES` is the list of `.proto` files that will be used in the generation procedure.
- `PROTO_INCLUDES` is the list of directories that will be searched for dependencies.
- `OUTPUT_DIRECTORY` is the directory where the generated files will be put. By default, the current directory (while evaluating the function) is used.
- `OUTPUT_HEADERS` can be used to specify a variable that will hold the list of headers created by the function. This list can be useful for custom project install rules.
- `OUTPUT_TARGETS` can be used to specify a variable that will hold the list of targets created by the function. This list can be useful for custom project install rules.

See also The qtgrpcgen Tool.