简体   繁体   中英

cmake error Cannot find source file when file is generated by EXEC_PROGRAM

I want to use protobuf in my project without install, so I copy the libs and headers from others, and create a FindProtobuf.cmake which include function named protobuf_generate_cpp, it generate source code files as i expect, but add_library cannot found the files, this is error:

Cannot find source file:

    build/basic_types.pb.h 

  Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
  .hpp .hxx .in .txx

i use cmake 3.13.4 in ubuntu 18.04.1

the files is generated exactly in directory of build, and them can be show by

message(STATUS "header file: " ${PROTO_HDRS})

this is the function of protobuf_generate_cpp:

function(protobuf_generate_cpp PROTO_SRCS PROTO_HDRS)
  set(SRCS_)
  set(HDRS_)
  math(EXPR idx_max "${ARGC}-1")

  foreach(idx RANGE 2 ${idx_max})
    set(FILE_PATH ${ARGV${idx}})

    if(NOT EXISTS ${FILE_PATH})
      set(FILE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/${FILE_PATH}")
      if(NOT EXISTS ${FILE_PATH})
        message(FATAL_ERROR "File ${FILE_PATH} is not exists!")
      endif()
    endif()

    get_filename_component(FILE_NAME ${FILE_PATH} NAME_WE)
    get_filename_component(ABSOLUTE_FILE_NAME ${FILE_PATH} ABSOLUTE)
    get_filename_component(FILE_PATH ${FILE_PATH} PATH)

    EXEC_PROGRAM("protoc --proto_path ${FILE_PATH} --cpp_out ${CMAKE_BINARY_DIR} ${ABSOLUTE_FILE_NAME}")

    list(APPEND SRCS_ "${CMAKE_BINARY_DIR}/${FILE_NAME}.pb.cc ")
    list(APPEND HDRS_ "${CMAKE_BINARY_DIR}/${FILE_NAME}.pb.h ")
  endforeach()

  set(PROTO_SRCS ${SRCS_} PARENT_SCOPE)
  set(PROTO_HDRS ${HDRS_} PARENT_SCOPE)
endfunction(protobuf_generate_cpp)
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS
  basic_types.proto
  calibration_parameters.proto
)
add_library(proto STATIC
  ${PROTO_HDRS}
  ${PROTO_SRCS}
)
add_library(proto STATIC
  ${PROTO_HDRS}
  ${PROTO_SRCS}
)

just only can include source files.

you can try this code below,

add_library(proto STATIC
  ${PROTO_SRCS}
)
target_include_directory(proto 【header's dir】)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM