如何将proto文件添加到Qt项目中

问题描述 投票:0回答:1

我想将此文件

tutorial.proto
添加到我的 qmake Qt 项目中,该项目是我从教程中复制的:

syntax="proto3";

package qtprotobuf.tutorial;

message EchoRequest {
  string message = 1;
}

message EchoResponse {
  string message = 1;
}

service EchoService {
  rpc Echo(EchoRequest) returns (EchoResponse);
}

我尝试连接 ProtoBuff 库本身,一切似乎都正常,至少该项目正在整合。

我的专业档案:

QT = websockets

TARGET = server
CONFIG   += console
CONFIG   -= app_bundle
CONFIG += c++17 cmdline

TEMPLATE = app

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp \
        server.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += \
    server.h

target.path = $$[QT_INSTALL_EXAMPLES]/websockets/echoserver
INSTALLS += target

LIBS += -lprotobuf

DISTFILES += \
    tutorial.prot
c++ qt protocol-buffers qmake
1个回答
0
投票
QT += network

TARGET = server
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

# Path to your .proto file
PROTO_FILE = tutorial.proto

# Path to generated C++ files
PROTO_OUT_DIR = $$OUT_PWD/protobuf_generated
# Create the output directory if it doesn't exist
!exists($$PROTO_OUT_DIR) {
    system(mkdir $$PROTO_OUT_DIR)
}

# Command to generate C++ files from .proto file
PROTOC = protoc
PROTO_SRC = $$PWD/$$PROTO_FILE
PROTO_INC = -I$$PWD
PROTO_FLAGS = --cpp_out=$$PROTO_OUT_DIR $$PROTO_SRC

# Run the protoc command
system($$PROTOC $$PROTO_INC $$PROTO_FLAGS)

# Add generated files to your project
PROTO_GEN_FILES = $$files($$PROTO_OUT_DIR/*.pb.cc)
PROTO_GEN_HEADERS = $$files($$PROTO_OUT_DIR/*.pb.h)
SOURCES += $$PROTO_GEN_FILES
HEADERS += $$PROTO_GEN_HEADERS

# Include generated files in the include path
INCLUDEPATH += $$PROTO_OUT_DIR

# Add protobuf library
LIBS += -lprotobuf

此 .pro 文件将:

  • 定义 .proto 文件、输出目录、protoc 命令的变量并包含路径。
  • 使用 protoc 编译器从 .proto 文件生成 C++ 文件。
  • 将生成的 .pb.cc 和 .pb.h 文件添加到项目的源文件和头文件中。
  • 将输出目录包含在包含路径中。
  • ProtoBuf 库的链接。
© www.soinside.com 2019 - 2024. All rights reserved.