Skip to content

Instantly share code, notes, and snippets.

@shi-yan
Last active August 9, 2022 14:32
Show Gist options
  • Save shi-yan/a9b207a243e990bbbc0a to your computer and use it in GitHub Desktop.
Save shi-yan/a9b207a243e990bbbc0a to your computer and use it in GitHub Desktop.
Build Protobuf files as a prebuild step of qmake (support protobuf in Qt)
#both protopath and protos need to use the relative path.
#otherwise you will see this protobuf compiler message:
#You must specify a --proto_path which encompasses this file. Note that the proto_path must be an exact prefix
#of the .proto file names -- protoc is too dumb to figure out when two paths
# (e.g. absolute and relative) are equivalent (it's harder than you think).
PROTOPATH += ../../../../common/protocols/protos #this is the folder contains *.proto
PROTOPATHS =
for(p, PROTOPATH):PROTOPATHS += --proto_path=$${p}
PROTOS += ../../../../common/protocols/protos/xxx.proto #the actual *.proto files need to be compiled.
protobuf_decl.name = protobuf header
protobuf_decl.input = PROTOS
protobuf_decl.output = ${QMAKE_FILE_BASE}.pb.h
protobuf_decl.commands = protoc --cpp_out=$$PWD $${PROTOPATHS} ${QMAKE_FILE_NAME}
protobuf_decl.variable_out = GENERATED_FILES
QMAKE_EXTRA_COMPILERS += protobuf_decl
protobuf_impl.name = protobuf implementation
protobuf_impl.input = PROTOS
protobuf_impl.output = ${QMAKE_FILE_BASE}.pb.cc
protobuf_impl.depends = ${QMAKE_FILE_BASE}.pb.h
protobuf_impl.commands = $$escape_expand(\n)
protobuf_impl.variable_out = GENERATED_SOURCES
QMAKE_EXTRA_COMPILERS += protobuf_impl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment