PROTOBUF_GENERATE_CPP does not generate src and header files

My cmake configuration does not generate protobuf src and header files. I already checked if proto files can be found.

Cmakelists.txt cmake_minimum_required(VERSION 3.0.2) .. include(FindProtobuf REQUIRED) file(GLOB PROTO_DEF "${CMAKE_CURRENT_SOURCE_DIR}/protobuf/*/*.proto") foreach(file ${PROTO_DEF}) if(EXISTS ${file}) MESSAGE("YES") else() MESSAGE("NO") endif() endforeach() SET(PROTOBUF_GENERATE_CPP_APPEND_PATH PROTOBUF) SET(PROTOBUF_PROTOC_EXECUTABLE protoc.exe) .. PROTOBUF_GENERATE_CPP(PROTO_SRC PROTO_INCL ${PROTO_DEF}) .. add_library(${PROJECT_NAME} STATIC ${INCLUDES} ${INTERNAL_INCLUDES} ${SRC} ${PROTO_SRC} ${PROTO_INCL}) target_link_libraries(${PROJECT_NAME} ${PROTOBUF_LIBRARIES}) 

I checked FindProtobuf.cmake and halfway through:

 foreach(FIL ${ARGN}) get_filename_component(ABS_FIL ${FIL} ABSOLUTE) get_filename_component(FIL_WE ${FIL} NAME_WE) list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc") list(APPEND ${HDRS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h") MESSAGE(1 ${CMAKE_CURRENT_BINARY_DIR}) MESSAGE(2 ${_protobuf_include_path}) MESSAGE(3 ${ABS_FIL}) MESSAGE(4 ${PROTOBUF_PROTOC_EXECUTABLE}) add_custom_command( OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc" "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h" COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL} DEPENDS ${ABS_FIL} COMMENT "Running C++ protocol buffer compiler on ${FIL}" VERBATIM ) endforeach() 

You can see that I added 4 message commands, the script reaches this point, and the variables show good values. Proto files matter to the library, so the command must be executed !?

Any ideas on this issue?

Update

replacing add_custom_command with

 EXEC_PROGRAM(${PROTOBUF_PROTOC_EXECUTABLE} ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL} 

creates source and header files, do I have to manually activate custom_commmands?

Regards Auke

+6
source share
2 answers

The add_custom_command command is run (for my project) at compile time. Adding

 SET_SOURCE_FILES_PROPERTIES(${PROTO_SRC} ${PROTO_INCL} PROPERTIES GENERATED TRUE) 

gives cmake information that the files will be generated.

+3
source

Alternative if you have an external proto folder:

 file(GLOB PROTOBUF_FILELIST ${PROTO_INCLUDE_DIR}/*.proto) foreach( proto_file ${PROTOBUF_FILELIST} ) get_filename_component(proto_name ${proto_file} NAME_WE) get_filename_component(proto_path ${PROTO_INCLUDE_DIR} ABSOLUTE) set_source_files_properties("${proto_path}/${proto_name}.pb.cc" "${proto_path}/${proto_name}.pb.h" PROPERTIES GENERATED TRUE) endforeach() 
+1
source

Source: https://habr.com/ru/post/984436/


All Articles