Qt6 GRPC 中的 CMake 命令

若要在项目中使用 Qt6::Grpc 模块,应调用以下 CMake 命令:

find_package(Qt6 REQUIRED COMPONENTS Grpc)

target_link_libraries(mytarget PRIVATE Qt6::Grpc)

您可以使用qt_add_grpcCMake 命令,为项目隐式调用Qt GRPC 代码生成功能。

若要使用 gRPC™Qt GRPC 服务并将其与您的程序链接,请参考以下示例:

cmake_minimum_required(VERSION 3.16...3.22)
project(MyProject)

find_package(Qt6 REQUIRED COMPONENTS Protobuf Grpc)
qt_standard_project_setup()

qt_add_executable(MyApp main.cpp)

qt_add_protobuf(MyApp
    PROTO_FILES
        path/to/messages.proto
)

qt_add_grpc(MyApp CLIENT
    PROTO_FILES
        path/to/service.proto
)

target_link_libraries(MyApp PRIVATE Qt6::Protobuf Qt6::Grpc)

上述示例调用qt_add_grpc() CMake函数,为给定protobuf模式中的service 部分启动Qt GRPC 代码生成。

注意:如果 Protobuf 模式中还包含message 定义,则也应调用qt_add_protobuf() CMake 函数以启动Qt Protobuf 代码生成。

由于我们复用了可执行目标,所有生成的文件都将附加到该目标中,并且包含目录也会相应地更新。

另请参阅 《CMake 命令参考》。

qt_add_grpc

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

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