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++ 服务

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