I need to compile protocol buffer .proto files to .pb.cc,.pb.h files. There is a program for this conversion.
protoc test.proto --cpp_out .
How can I add such a generic rule in cmake? I can do this with add_custom_command. But I have to this for every .proto file. Is there a better way to do this?
It looks like CMake's FindProtobuf
module provides this functionality via the function PROTOBUF_GENERATE_CPP
.
You can pass multiple .proto files in the one call, e.g.
file(GLOB ProtoFiles "${CMAKE_CURRENT_SOURCE_DIR}/*.proto")
PROTOBUF_GENERATE_CPP(ProtoSources ProtoHeaders ${ProtoFiles})
Note that even though the CMakeLists.txt file which calls find_package(Protobuf)
could be the top-level one, the CMakeLists.txt file(s) which invoke the function would need to be in the same directory as the .proto files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With