qt_add_grpc

使用 Protobuf 模式生成基于 Qt 的 C++ 服务

注意: 此命令为技术预览版,在未来版本中可能会有所更改。

此命令在 Qt 6.5 中引入。

通常qtgrpcgen 是通过qt_add_grpc CMake 宏调用的。

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 代码生成。

源文件由qtgrpcgen 创建并添加到目标中。如果目标已存在,文件将添加到目标源文件列表中。如果目标不存在,则会将其创建为库,您必须链接到该库。

使用 gRPCqt_add_grpc 库是指服务调用中使用的 protobuf 符号。这意味着您需要在qt_add_protobufqt_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 生成 服务的 QML 客户端 API。该参数需要 Qt XML 组件。gRPC GrpcQuick
    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.testOUTPUT_DIRECTORY/io/qt/test/ 目录下。
  • COPY_COMMENTS 将 文件中的注释复制到生成的代码中。.proto
  • EXPORT_MACRO 仅适用于从<target> 创建新的共享库该选项指定生成代码中使用的导出宏的基本名称。最终的宏名构造为 。如果未设置该选项,则目标名称为 。QPB_<EXPORT_MACRO>_EXPORT EXPORT_MACRO

    请阅读 "创建共享库 "获取更多深入信息。

  • OUTPUT_HEADERS 指定一个变量,用于存储生成的头文件列表。该列表可用于定义自定义项目安装规则。
  • OUTPUT_TARGETS 指定一个变量,用于存储生成的目标列表。该列表有助于定义自定义项目安装规则。
  • HEADER_GUARD 指定用于防止生成的头文件被多重包含的机制。可能的值是 , 。默认值为 。将该选项设置为 会生成现代 pragma 头文件防护:pragma filename filename pragma
    #pragma once
    ...

    省略该选项或将该选项设置为filename 会生成ifdef 封装保护,并使用".proto "文件名作为保护后缀:

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

    根据项目结构选择首选的保护方式。

另请参阅 qtgrpcgen 工具

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