title: Generated PROTOBUF_INTERNAL_EXPORT does not include subdirectory for imports
body: <p>I have struggled so much trying to get my proto files to compile correctly for use in my C++ project. I've had to try and fail from at least 8 different ways of doing it from several stackoverflow posts.</p>
  <p>Currently, I get an error:</p>
  <pre><code>‘PROTOBUF_INTERNAL_EXPORT_messages_2fbot_5farrived_2eproto’ does not name a type; did you mean ‘PROTOBUF_INTERNAL_EXPORT_bot_5farrived_2eproto’?
  </code></pre>
  <p>extern PROTOBUF_INTERNAL_EXPORT_messages_2fbot_5farrived_2eproto ::PROTOBUF_NAMESPACE_ID::internal::SCCInfo&lt;0&gt; scc_info_BotArrived_messages_2fbot_5farrived_2eproto;</p>
  <p>If I delete the <code>messages_2f</code> from the generated extern, it works. Why is it not generating things with the subdirectory from which I import, correctly?</p>
  <p>My proto files are stored:</p>
  <pre><code>project
      protos
          message   # This imports all the concrete types using import &quot;messages/&lt;concretetype&gt;
      messages
          bot_arrived.proto
          .... other concrete types
      CmakeLists.txt
      ...
          
  </code></pre>
  <p>My current Frakenstein-ed cmake file section, where the files are generated, looks like so:</p>
  <pre><code># Generated Proto files
  file(GLOB_RECURSE proto_files &quot;${CMAKE_CURRENT_SOURCE_DIR}/*.proto&quot;)
  
  # Create an include path for each file specified
  if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
      foreach(FIL ${proto_files})
          get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
          get_filename_component(ABS_PATH ${ABS_FIL} PATH)
          list(FIND protobuf_include_path ${ABS_PATH} contains_already)
          if(${contains_already} EQUAL -1)
              list(APPEND protobuf_include_path -I ${ABS_PATH})
          endif()
      endforeach()
  else()
      set(protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
  endif()
  
  message(STATUS &quot;Proto Include Path: ${protobuf_include_path}&quot;)
  
  set(proto_srcs)
  set(proto_hdrs)
  set(grpc_proto_srcs)
  set(grpc_proto_hdrs)
  set(grpc_generated_include_paths &quot;${CMAKE_CURRENT_BINARY_DIR}&quot; CACHE STRING &quot;Will be needed in the include path of targets that consume this lib&quot;)
  
  foreach(FIL ${proto_files})
      get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
      get_filename_component(FIL_WE ${FIL} NAME_WE)
      get_filename_component(DIR ${FIL} DIRECTORY)
      file(RELATIVE_PATH REL_DIR &quot;${CMAKE_CURRENT_SOURCE_DIR}/protos/&quot; ${DIR})
  
      if(&quot;${REL_DIR}&quot; STREQUAL &quot;&quot;)
          set(proto_src &quot;${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.cc&quot;)
          set(proto_hdr &quot;${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.pb.h&quot;)
          set(grpc_proto_src &quot;${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.cc&quot;)
          set(grpc_proto_hdr &quot;${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.grpc.pb.h&quot;)
          set(grpc_out_dir &quot;${CMAKE_CURRENT_BINARY_DIR}&quot;)
      else()
          set(proto_src &quot;${CMAKE_CURRENT_BINARY_DIR}/${REL_DIR}/${FIL_WE}.pb.cc&quot;)
          set(proto_hdr &quot;${CMAKE_CURRENT_BINARY_DIR}/${REL_DIR}/${FIL_WE}.pb.h&quot;)
          set(grpc_proto_src &quot;${CMAKE_CURRENT_BINARY_DIR}/${REL_DIR}/${FIL_WE}.grpc.pb.cc&quot;)
          set(grpc_proto_hdr &quot;${CMAKE_CURRENT_BINARY_DIR}/${REL_DIR}/${FIL_WE}.grpc.pb.h&quot;)
          set(grpc_out_dir &quot;${CMAKE_CURRENT_BINARY_DIR}/${REL_DIR}&quot;)
      endif()
  
      if( NOT EXISTS &quot;${grpc_out_dir}&quot; AND NOT IS_DIRECTORY &quot;${grpc_out_dir}&quot;)
          file(MAKE_DIRECTORY &quot;${grpc_out_dir}&quot;)
      endif()
  
      list(FIND grpc_generated_include_paths ${grpc_out_dir} contains_already)
      if(${contains_already} EQUAL -1)
          list(APPEND grpc_generated_include_paths ${grpc_out_dir})
      endif()
  
      list(APPEND proto_srcs &quot;${proto_src}&quot;)
      list(APPEND proto_hdrs &quot;${proto_hdr}&quot;)
      list(APPEND grpc_proto_srcs &quot;${grpc_proto_src}&quot;)
      list(APPEND grpc_proto_hdrs &quot;${grpc_proto_hdr}&quot;)
  
      add_custom_command(
          OUTPUT &quot;${proto_src}&quot; &quot;${proto_hdr}&quot; &quot;${grpc_proto_src}&quot; &quot;${grpc_proto_hdr}&quot;
          COMMAND ${Protobuf_PROTOC_EXECUTABLE}
          ARGS --grpc_out &quot;${grpc_out_dir}&quot;
          --cpp_out &quot;${grpc_out_dir}&quot;
          --plugin=protoc-gen-grpc=${GRPC_PLUGIN}
          ${protobuf_include_path} ${ABS_FIL}
          DEPENDS ${ABS_FIL} ${Protobuf_PROTOC_EXECUTABLE}
          COMMENT &quot;Running gRPC C++ protocol buffer compiler on ${FIL}. Output going to ${grpc_out_dir}&quot;
          VERBATIM)
  endforeach()
  
  set_source_files_properties(${proto_srcs} ${proto_hdrs} PROPERTIES SKIP_AUTOMOC TRUE)
  set_source_files_properties(${proto_srcs} ${proto_hdrs} PROPERTIES GENERATED TRUE)
  
  message(STATUS &quot;Proto generated sources: ${proto_srcs}&quot;)
  message(STATUS &quot;Proto generated headers: ${proto_hdrs}&quot;)
  message(STATUS &quot;gRPC generated sources: ${grpc_proto_srcs}&quot;)
  message(STATUS &quot;gRPC generated headers: ${grpc_proto_hdrs}&quot;)
  message(STATUS &quot;gRPC generated include paths ${grpc_generated_include_paths}&quot;)
  </code></pre>