简体   繁体   中英

Does cmake support delay-loading dlls on Windows?

I'm converting a MSBuild project to a cmake project (IDE is Visual Studio 2022).

MSBuild properties page allows setting selected dlls to be "delay-loaded" but searching online leaves me finding nothing about a similar flag or configuration setting to enable this in my CMakeLists.txt for the cmake build when I setup my target_link_libraries().

Am I missing a way to do this with a CMAKE_ variable or other command, or is this simply not a supported feature of cmake since it evolved as a Linux tool and delay-loading isn't really a thing there?

This will be possible in CMake 3.24, which is on its second release candidate as I write this answer.

foreach (lang IN ITEMS C CXX)
  if (CMAKE_${lang}_COMPILER_ID STREQUAL "MSVC")
    set(CMAKE_${lang}_LINK_LIBRARY_USING_delayload "/DELAYLOAD:<LIBRARY>")
  else ()
    set(CMAKE_${lang}_LINK_LIBRARY_USING_delayload_SUPPORTED FALSE)
  endif ()
endforeach ()

target_link_libraries(lib2 PRIVATE "$<LINK_LIBRARY:delayload,lib1>")

I'm not sure what the best practices surrounding these will be. One might need to include the above code snippet in their export files under some circumstances. Probably not an issue for executables, but might well be for libraries.

https://cmake.org/cmake/help/latest/variable/CMAKE_LINK_LIBRARY_USING_FEATURE.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM