title: Generate protobuf files for a library target in CMAKE
body: <p>In the following project structure:</p>
  <pre><code>root/
  ├─ CMakeLists.txt
  ├─ protocol/
  │  ├─ msg.proto
  │  ├─ CMakeLists.txt
  ├─ app/
  │  ├─ main.cpp
  │  ├─ CMakeLists.txt
  
  </code></pre>
  <p>I could generate the protobuf files. However, once the <code>app</code> target is added into the project, the configuration step fails, because the protobuf files are not generated anymore.</p>
  <p>root CMakeLists.txt:</p>
  <pre><code>cmake_minimum_required(VERSION 3.18.4) 
  project(
    root
    VERSION 0.1
    LANGUAGES CXX
  )
  
  add_subdirectory(protocol)
  add_subdirectory(app) 
  </code></pre>
  <p>protocol CMakeLists.txt:</p>
  <pre><code>add_library(protocol)
  target_include_directories(protocol
    PUBLIC
    .
    ${CMAKE_CURRENT_BINARY_DIR}
    ${Protobuf_INCLUDE_DIRS}
  )
  find_package(Protobuf REQUIRED)
  set(PROTO_SOURCES msg.proto)
  protobuf_generate_cpp(LIB_SOURCES LIB_HEADERS ${PROTO_SOURCES} )
  target_link_libraries(protocol ${Protobuf_LIBRARIES})
  target_sources(protocol
    PUBLIC
    ${LIB_HEADERS}
    PRIVATE
    ${LIB_SOURCES}
  )
  
  </code></pre>
  <p>app CMakeLists.txt:</p>
  <pre><code>add_library(app)
  target_link_libraries(app PUBLIC protocol)
  target_include_directories(app PUBLIC .)
  target_sources(app
    PRIVATE
    main.cpp
  )
  
  </code></pre>
  <p>The way I understand it is that somehow I have to tell CMAKE the dependency, but as far as I know <code>target_link_libraries</code> should tell exactly that.</p>
  <p>Another step I could take is to tell the <code>app</code> target that the headers in the <code>protocol</code> library are generated at build, so they can be generated before the dependant target compiles. How can it be done?</p>
  <p>Many other questions reference similar problems, but none that I found had library dependencies in mind.</p>
  <hr />
  <p>Update:</p>
  <p>After moving <code>${LIB_HEADERS}</code> from <code>PUBLIC</code> to <code>PRIVATE</code>:</p>
  <pre><code>add_library(protocol)
  target_include_directories(protocol
    PUBLIC
    .
    ${CMAKE_CURRENT_BINARY_DIR}
    ${Protobuf_INCLUDE_DIRS}
  )
  find_package(Protobuf REQUIRED)
  set(PROTO_SOURCES msg.proto)
  protobuf_generate_cpp(LIB_SOURCES LIB_HEADERS ${PROTO_SOURCES} )
  target_link_libraries(protocol ${Protobuf_LIBRARIES})
  target_sources(protocol
    PRIVATE
    ${LIB_HEADERS}
    ${LIB_SOURCES}
  )
  
  </code></pre>
  <p>the configure step is successful, the msg.pb.h files are generated into the <code>protocol</code> subfloder in <code>build</code>; but make fails with the following error:</p>
  <pre><code>main.cpp:29:10: fatal error: protocol/msg.pb.h: No such file or directory
     29 | #include &quot;protocol/msg.pb.h&quot;
        |   
  </code></pre>
  <p>So how can the headers be provided from target <code>protocol</code> in this setup?</p>