Visual Studio /delayload with CMake

CMAKE_LINK_LIBRARY_USING_FEATURE doesn’t have a feature to delay loading import for MSVC-like compilers flag like /delayload. Nonetheless, /delayload can be accomplished in a compact way as in the following example:

add_library(example SHARED lib.c)
add_executable(main main.c)

target_link_libraries(main PRIVATE example)

if(MSVC)
  set_property(TARGET example PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS true)
  target_link_libraries(main PRIVATE delayimp)
  target_link_options(main PRIVATE "/DELAYLOAD:$<TARGET_FILE_BASE_NAME:example>.dll")
endif()