qt_add_grpc
使用 protobuf 模式生成基于 Qt 的 C++ 服务
该命令定义在 Qt6 软件包的 Grpc 组件中。请使用以下命令加载该软件包:
find_package(Qt6 REQUIRED COMPONENTS Grpc)该命令于 Qt 6.5 版本中引入。
通常通过 CMake 宏qt_add_grpc 调用qtgrpcgen 。
语法概述
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>]
)注意: 目前仅 支持CLIENT 的代码生成。
如果禁用了无版本号的命令,请改用 `qt6_add_grpc() `。该命令支持与本命令相同的参数集。
说明
源文件由qtgrpcgen 生成并添加到目标中。如果目标已存在,这些文件将被添加到目标的源文件列表中;如果目标不存在,则会将其作为库创建,您必须将其链接到程序中。
使用 gRPC™ 使用qt_add_grpc 命令生成的库引用了服务调用中使用的protobuf符号。这意味着您需要在qt_add_protobuf() 和qt_add_grpc()调用中使用同一个目标,或者将生成的protobuf库链接到gRPC 库中。
考虑到上述说明,您可以为 protobuf 和gRPC 客户端代码复用同一个目标:
# 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
)如果您希望使用独立的目标,则需要将gRPC 客户端目标链接到 protobuf 目标:
# 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)参数
PROTO_FILES指定代码生成过程所使用的.proto文件列表。PROTO_INCLUDES指定用于搜索 protobuf 依赖项的目录列表。注意:
PROTO_FILES的位置 会被默认视为 protobuf 包含路径的一部分。QML生成针对gRPC 服务的QML客户端API。该参数需要GrpcQuickQt组件。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定义生成的文件将放置的目录。默认情况下,使用当前构建目录。GENERATE_PACKAGE_SUBFOLDERS使用.proto文件中的包名指定符来创建生成的文件的文件夹结构。例如,如果包定义为:package io.qt.test,则生成的文件将放置在OUTPUT_DIRECTORY/io/qt/test/中。COPY_COMMENTS将.proto文件中的注释复制到生成的代码中。EXPORT_MACRO仅在从<target> 创建新共享库时生效。此选项指定生成代码中使用的导出宏的基名。最终的宏名称构建为QPB_<EXPORT_MACRO>_EXPORT。如果未设置此选项,则使用目标名称作为EXPORT_MACRO。请参阅《创建共享库》以获取更深入的信息。
OUTPUT_HEADERS指定一个变量,用于存储生成的头文件列表。该列表可用于定义自定义项目安装规则。OUTPUT_TARGETS指定一个变量,用于存储生成的目标列表。该列表可用于定义自定义项目安装规则。HEADER_GUARD指定用于防止生成的头文件被多次包含的机制。可能的取值为pragma、filename。默认值为filename。将该选项设置为pragma将生成现代的 pragma 头文件保护机制:#pragma once ...省略该选项或将其设置为
filename时,将生成ifdef包装式保护机制,并使用 '.proto' 文件名作为保护机制的中缀:#ifdef MYMESSAGES_QPB_H #define MYMESSAGES_QPB_H ... #endif // MYMESSAGES_QPB_H请根据您的项目结构选择合适的保护符样式。
另请参阅 “qtgrpcgen 工具”。
© 2026 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.