CMake: How to add my own general compilation rule?

I need to compile .proto protocol files of .pb.cc, .pb.h files. There is a program for this conversion.

protoc test.proto --cpp_out . 

How to add such a general rule to cmake? I can do this with add_custom_command. But I owe this to every .proto file. Is there a better way to do this?

+7
source share
1 answer

The CMake FindProtobuf module FindProtobuf provide this function with the PROTOBUF_GENERATE_CPP function.

You can transfer multiple .proto files in one call, for example

 file(GLOB ProtoFiles "${CMAKE_CURRENT_SOURCE_DIR}/*.proto") PROTOBUF_GENERATE_CPP(ProtoSources ProtoHeaders ${ProtoFiles}) 

Note that even if the CMakeLists.txt file that calls find_package(Protobuf) may be at the top, the CMakeLists.txt file that calls this function must be in the same directory as the .proto files.

+6
source

All Articles