MSVC: DELAYLOAD example

CMAKE_LINK_LIBRARY_USING_FEATURE
is a nice addition to CMake.

While there isn’t a delayload feature for MSVC-like compilers, as noted by Marc below /delayload can be accomplished in a compact way as in the following example:

add_library(my SHARED my.c)
add_executable(main main.c)

target_link_libraries(main PRIVATE my)

if(MSVC)
  target_link_libraries(main PRIVATE delayimp)
  target_link_options(main PRIVATE "/DELAYLOAD:$<TARGET_FILE_BASE_NAME:my>.dll")
endif()

Currently, what you want is not possible using $<LINK_LIBRARY> because, currently, there is no way to express the runtime library name as part of the CMAKE_LINK_LIBRARY_USING_FEATURE variable.

But you can simplify your link option by using $<TARGET_FILE_BASE_NAME> genex:

target_link_options(main PRIVATE "/DELAYLOAD:$<TARGET_FILE_BASE_NAME:a>.dll")
target_link_libraries(main PRIVATE a delayimp)
1 Like

OK I think that will be adequate. There are other custom little Windows things I have to link like Winsock libraries etc. so this just becomes another one of those.

Yes especially that $<TARGET_FILE_BASE_NAME> I was forgetting, then it’s all effectively programmatic and so no new functionality is needed after all.

While in many cases that genex just emits the same name, sometimes target names are customized, and at least the genex makes the use of the library name stand out.