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

- Qt 6.11
- Qt GRPC
- CMake Commands in Qt6 GRPC
- qt_add_grpc
- Qt 6.11.0 Reference Documentation

CMake Commands

# qt_add_grpc

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

This command was introduced in Qt 6.5.

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

     qt_add_grpc(<target> <CLIENT|SERVER>
         PROTO_FILES <file> ...
         [PROTO_INCLUDES <path> ...]
         [QML]
         [OUTPUT_DIRECTORY <dir>]
         [GENERATE_PACKAGE_SUBFOLDERS]
         [COPY_COMMENTS]
         [EXPORT_MACRO <infix>]
         [OUTPUT_HEADERS <var>]
         [OUTPUT_TARGETS <var>]
         [HEADER_GUARD <pragma|filename>]
     )

Note: Only `CLIENT` code generation is currently supported.

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

- `PROTO_FILES` specifies the list of .proto files used by the code generation procedure.
- `PROTO_INCLUDES` specifies the list of directories to be searched for protobuf dependencies.

  Note: The location of the `PROTO_FILES` is implicitly considered part of the protobuf include path.

- `QML` generates the QML client API for the gRPC service. The argument requires the `GrpcQuick` Qt component.

       find_package(Qt6 6.8 REQUIRED COMPONENTS Quick Protobuf ProtobufQuick Grpc GrpcQuick)
       ...
       # Generate the protobuf types
       qt_add_protobuf(test_service_client
           PROTO_FILES
               test_service.protobuf
           QML
       )
       ...
       # Generate \gRPC client functionality
       qt_add_grpc(test_service_client CLIENT
           PROTO_FILES
               test_service.protobuf
           QML
       )

- `OUTPUT_DIRECTORY` defines the directory where the generated files will be placed. By default, the current build directory is used.
- `GENERATE_PACKAGE_SUBFOLDERS` uses the package name specifier from the `.proto` files to create the folder structure for the generated files. For example, if the package is defined as: `package io.qt.test`, the generated files will be placed in OUTPUT_DIRECTORY/io/qt/test/.
- `COPY_COMMENTS` copies comments from the `.proto` files into the generated code.
- `EXPORT_MACRO` applies only when creating a new shared library from the <target>. This option specifies the base name for the export macro used in the generated code. The final macro name is constructed as `QPB_<EXPORT_MACRO>_EXPORT`. If this option is not set, the target name is used as `EXPORT_MACRO`.

  Read Creating Shared Libraries for further in-depth information.

- `OUTPUT_HEADERS` specifies a variable that will store the list of generated headers. This list can be useful for defining custom project install rules.
- `OUTPUT_TARGETS` specifies a variable that will store the list of generated targets. This list can be useful for defining custom project install rules.
- `HEADER_GUARD` specifies the mechanism used for guarding generated header files from multiple inclusion. Possible values are `pragma`, `filename`. Defaults to `filename`. Setting the option to `pragma` generates the modern pragma header guard:

       #pragma once
       ...

  Omitting the option or setting the option to `filename` generates the `ifdef` wrapping guard, and uses the '.proto' filename as the guard infix:

       #ifdef MYMESSAGES_QPB_H
       #define MYMESSAGES_QPB_H
       ...
       #endif // MYMESSAGES_QPB_H

  Select the preferred guard style according to your project structure.

See also The qtgrpcgen Tool.

CMake Commands