Libraries and Native Visualization Files (.natvis)

I have a header only library that provides a natvis file to users. This is useful to users of my library on MSVC in particular. Since it provides a friendlier debugging interface for custom containers.

If I was shipping a SHARED or MODULE type library this would be easy since I could just use the appropriate link command for the MSVC compiler.

target_link_options(foobar PRIVATE 
    /NATVIS:${CMAKE_CURRENT_LIST_DIR}/foobar.natvis
)

However, this gets complicated for STATIC / HEADER only libraries. I also want it to work with both Ninja and the Visual Studio generator. I also want it to work cleanly with my library being absorbed via add_subdirectory or find_package.

What is the best way to support natvis files cleanly in CMake?

CMake probably needs to learn NatVis support itself. https://gitlab.kitware.com/cmake/cmake/-/issues/16874

1 Like