# MSVC: DELAYLOAD example

**URL:** https://discourse.cmake.org/t/msvc-delayload-example/6534
**Category:** Code
**Tags:** os:windows, comp:msvc
**Created:** [September 20, 2022, 4:45pm UTC](https://discourse.cmake.org/t/msvc-delayload-example/6534 "2022-09-20T16:45:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [September 20, 2022, 4:45pm UTC](https://discourse.cmake.org/t/msvc-delayload-example/6534/1 "2022-09-20T16:45:12Z")

</div>

[CMAKE\_LINK\_LIBRARY\_USING\_FEATURE](https://cmake.org/cmake/help/latest/variable/CMAKE_LINK_LIBRARY_USING_FEATURE.html)  
is a nice addition to CMake.

While there isn’t a [delayload](https://learn.microsoft.com/en-us/cpp/build/reference/delayload-delay-load-import) feature for MSVC-like compilers, as noted by Marc below /delayload can be accomplished in a compact way as in the following example:

```cmake
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()

```

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [September 20, 2022, 5:02pm UTC](https://discourse.cmake.org/t/msvc-delayload-example/6534/2 "2022-09-20T17:02:08Z")

</div>

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:

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

```

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [September 20, 2022, 6:26pm UTC](https://discourse.cmake.org/t/msvc-delayload-example/6534/3 "2022-09-20T18:26:51Z")

</div>

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.
