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)

上の例では、PROTO_FILES オプションに渡されたパスで定義されたメッセージタイプを含むMyMessages というライブラリを生成しています。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.