Unexpected behavior when using CMake and Visual Studio


I have a simple project that is using protobuf to generate some bindings and then include them in a static library using Visual Studio/MSVC. The CMakeLists.txt section looks like this:

protobuf_generate(
    OUT_VAR PROTO_GENERATED_FILES
    PROTOS types.proto
    PROTOC_OUT_DIR "${CMAKE_CURRENT_LIST_DIR}/generated"
)

add_library(types STATIC ${PROTO_GENERATED_FILES})

target_link_libraries(types PRIVATE protobuf::libprotobuf)

This behaves as expected and after building I end up with the generated cpp bindings in the location I expect them:

enter image description here

The library then builds and dumpbin confirms I have the generated symbols in my library. So far, so good. I then have two scenarios in which I can open this solution in VS:

  1. By opening the directory containing my CMakePresets.json so that VS loads and configures the solution.
  2. By running cmake --preset <preset_name> and opening the generated solution.

2 works as expected, 1 does not.

With option 2, if I open the generated solution file I can see the generated source included in my library:

enter image description here

This means I can browse the generated source from within VS as it is included as part of my types library.

With the 1st option, if I open the folder, let cmake configure then switch to CMake target view, I do not see the generated source:

enter image description here

As mentioned, building works as expected, but given this generated source is part of my types library, I would expect it to appear as it does in option 2.

Is there an additional configuration step that I need in cmake to make this show in option 1 or is this a bug in CMake/VS itself?

Leave a Reply

Your email address will not be published. Required fields are marked *