title: CMake with protobuf file in subdirectory
body: <p>I have seen a lot of similar questions and answers, but until now it seems not so obvious to get it working. I am quite new to CMake and until now everything was easy except the integration with protocol buffers.</p>
  
  <p>I have a project with subdirectories, where each subdirectory has its own CMakeLists.txt  </p>
  
  <p>One of the subdirectory contains a .proto file. If the PROTOBUF_GENERATE_CPP macro is executed it generates the sources and the headers files. This macro is invoked from the CMakeLists.txt in the subdirectory containing the .proto file. </p>
  
  <p>It seems however the make file is not invoked because no sources are added to the target. I can not add the sources to the target, because the files do not exist, they exist after generation, so this results in an error when CMake runs.  </p>
  
  <p>Setting the file properties to generated seems also not to help. In general, before the build process starts the macro should have been run to generated the source files. </p>
  
  <p>How to do this, any working examples ? </p>
  
  <p>Example:</p>
  
  <p>./src/externals/protodef (from other repository, only contains .proto files)<br>
  ./src/generated (supposed for the generated c and header files by protoc)</p>
  
  <p>CMakeLists-1 (project root)</p>
  
  <pre><code>cmake_minimum_required (VERSION 2.6)
  PROJECT (prototest)
  
  ADD_SUBDIRECTORY("${PROJECT_SOURCE_DIR}/src/externals/protodef")
  ADD_SUBDIRECTORY("${PROJECT_SOURCE_DIR}/src")
  
  SET_SOURCE_FILES_PROPERTIES(${PROTO_SOURCES} ${PROTO_HEADERS} PROPERTIES GENERATED TRUE)
  
  ADD_EXECUTABLE(prototest ${PROTO_SOURCES} ${SOURCE} )
  TARGET_LINK_LIBRARIES(prototest ${EXTERNAL_LIBS} )
  
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  </code></pre>
  
  <p>CMakeLists-2 (src)</p>
  
  <pre><code>SET(SOURCE ${SOURCE}
    ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
  
    PARENT_SCOPE
  )
  </code></pre>
  
  <p>CMakeLists-3 (src/externals/protodef)</p>
  
  <pre><code>SET(PROTOBUF_PATH "D:/protobuf-3.0.0/" )
  SET(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "${PROTOBUF_PATH}")
  
  # Changing PROTO_SRCS and PROTO_HDRS does not work for setting the location 
  # of the generated files.
  # Those variable are ignored by CMake for compiling the proto files.
  # Using a dedicated CMakeLists.txt and settng CURRENT_BINARY dir is a 
  # workaround to get them where we want.
  SET(GENERATED_DIR ${PROJECT_SOURCE_DIR}/src/generated )
  SET(CMAKE_CURRENT_BINARY_DIR ${GENERATED_DIR} )
  
  
  INCLUDE(FindProtobuf)
  FIND_PACKAGE(Protobuf REQUIRED)
  
  PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS${CMAKE_CURRENT_SOURCE_DIR}/test1.proto)
  
  SET( EXTERNAL_LIBS ${PROTOBUF_PATH}/lib/libprotobuf.a PARENT_SCOPE)
  
  # Propagate sources to the parant project
  SET(PROTO_SOURCES ${PROTO_SRCS}
  PARENT_SCOPE
  )
  
  SET(PROTO_HEADERS ${PROTO_HDRS}
  PARENT_SCOPE
  )
  </code></pre>