Comandi CMake in Qt6 Protobuf
Per utilizzare il modulo Qt6::Protobuf nel proprio progetto, è necessario eseguire i seguenti comandi CMake:
find_package(Qt6 REQUIRED COMPONENTS Protobuf)
target_link_libraries(mytarget PRIVATE Qt6::Protobuf)È possibile utilizzare il comando CMake ` qt_add_protobuf ` per avviare implicitamente la generazione del codic Qt Protobuf e per il proprio progetto.
Utilizzo di 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)Nell'esempio sopra riportato, generiamo una libreria denominata MyMessages, che contiene i tipi di messaggio definiti nei percorsi passati all'opzione PROTO_FILES. L'opzione GENERATE_PACKAGE_SUBFOLDERS serve a generare una struttura di cartelle per i file generati. L'opzione PROTO_INCLUDES indica a protoc di cercare le dipendenze o le importazioni nelle directory specificate. Creiamo un target per un eseguibile denominato MyApp, che colleghiamo alla libreria MyMessages.
Vedi anche il Riferimento ai comandi CMake.
Genera codice sorgente C++ basato su Qt utilizzando uno schema protobuf. |
© 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.