Qt6 Protobuf 中的 CMake 命令

要在项目中使用 Qt6::Protobuf 模块,需要调用以下 CMake 命令:

find_package(Qt6 REQUIRED COMPONENTS Protobuf)

target_link_libraries(mytarget PRIVATE Qt6::Protobuf)

你可以使用qt_add_protobufCMake 命令为你的项目隐式调用Qt Protobuf 代码生成。

使用 qt_add_protobuf

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

find_package(Qt6 REQUIRED COMPONENTS Protobuf)
qt_standard_project_setup()

qt_add_protobuf(MyMessages
    GENERATE_PACKAGE_SUBFOLDERS
    PROTO_FILES
        path/to/message.proto
        path/to/other_message.proto
    PROTO_INCLUDES
        /path/to/proto/include
)

qt_add_executable(MyApp main.cpp)

target_link_libraries(MyApp PRIVATE MyMessages)

在上面的示例中,我们生成了一个名为MyMessages 的库,其中包含了传给PROTO_FILES 选项的路径中定义的消息类型。GENERATE_PACKAGE_SUBFOLDERS 选项为生成的文件生成一个文件夹结构。PROTO_INCLUDES 选项告诉 protoc 在指定目录中查找依赖项或导入项。我们创建了一个名为MyApp 的可执行文件目标,并将其链接到MyMessages 库。

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

qt_add_protobuf

使用 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.