qt_add_grpc | Qt GRPC | Qt 6.9.0 (original) (raw)

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( <CLIENT|SERVER> PROTO_FILES ... [PROTO_INCLUDES ...] [QML] [OUTPUT_DIRECTORY ] [GENERATE_PACKAGE_SUBFOLDERS] [COPY_COMMENTS] [EXPORT_MACRO ] [OUTPUT_HEADERS ] [OUTPUT_TARGETS ] [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

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
)

#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

© 2025 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.