# Usage of TARGET\_RUNTIME\_DLLS generator expression

**URL:** https://discourse.cmake.org/t/usage-of-target-runtime-dlls-generator-expression/6355
**Category:** Usage
**Created:** [August 26, 2022, 3:21pm UTC](https://discourse.cmake.org/t/usage-of-target-runtime-dlls-generator-expression/6355 "2022-08-26T15:21:35Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ottmar-zittlau](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ottmar-zittlau/32/2713_2.png) [@ottmar-zittlau](https://discourse.cmake.org/u/ottmar-zittlau)
#### Post date: [August 26, 2022, 3:21pm UTC](https://discourse.cmake.org/t/usage-of-target-runtime-dlls-generator-expression/6355/1 "2022-08-26T15:21:35Z")

</div>

Hi,

I’d like to copy all the dlls that are required for running a target to the output directory.  
In particular, I want to use the locally built optimization library ipopt, which depends on Intel’s MKL library.

My CMakeLists.txt looks like this:

```auto
cmake_minimum_required(VERSION 3.0.0)
project(IpoptTest VERSION 0.1.0)

set(IPOPT_DIR "C:/Libs/Ipopt")
find_library(IPOPT "ipopt.dll" "${IPOPT_DIR}/lib")

add_executable(IpoptTest main.cpp OptiProblem.cpp)
target_include_directories(IpoptTest PRIVATE "${IPOPT_DIR}/include")
target_link_libraries(IpoptTest PRIVATE "${IPOPT}")

add_custom_command(TARGET IpoptTest POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_RUNTIME_DLLS:IpoptTest> $<TARGET_FILE_DIR:IpoptTest>
  COMMAND_EXPAND_LISTS
  ) 

```

The command to copy the dlls is taken from ([https://cmake.org/cmake/help/v3.21/manual/cmake-generator-expressions.7.html#genex:TARGET\_RUNTIME\_DLLS](https://cmake.org/cmake/help/v3.21/manual/cmake-generator-expressions.7.html#genex:TARGET_RUNTIME_DLLS)).

However, when I run it, I get the output

```auto
[build] EXEC : CMake error : cmake version 3.23.22060601-MSVC_2 [C:\Programmieren\Projekte\IpoptTest\build\IpoptTest.vcxproj]
[build] Usage: C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe -E <command> [arguments...]
[build] Available commands: 
[build] capabilities - Report capabilities built into cmake in JSON format
[build] cat <files>... - concat the files and print them to the standard output
....

```

Apparently CMake fails to execute the copy command.

Is there any way to have CMake automatically determine all the required dlls?  
There is an ipopt.pc file in a pkgconfig folder, which seems to contain the paths of libraries ipopt depends upon (however, it contains the .lib not the .dll files). Would it make my life easier, if I used pkgconfig?

Thanks in advance and best regards,  
oz

---

<div class="post-metadata">

### Author: ![TheNybbler](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thenybbler/32/2747_2.png) [@TheNybbler](https://discourse.cmake.org/u/TheNybbler)
#### Post date: [September 7, 2022, 4:27pm UTC](https://discourse.cmake.org/t/usage-of-target-runtime-dlls-generator-expression/6355/2 "2022-09-07T16:27:31Z")

</div>

I also have the same problem.  
Oddly the dependency dlls still seem to get copied though, but the overall build fails.

---

<div class="post-metadata">

### Author: ![buildSystemPerson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/buildsystemperson/32/2851_2.png) [@buildSystemPerson](https://discourse.cmake.org/u/buildSystemPerson)
#### Post date: [September 7, 2022, 8:45pm UTC](https://discourse.cmake.org/t/usage-of-target-runtime-dlls-generator-expression/6355/3 "2022-09-07T20:45:48Z")

</div>

FWIW these links helped me get TARGET\_RUNTIME\_DLLS working in my project. They cover all the important aspects of this CMake feature.

> <https://stackoverflow.com/questions/71250816/cmake-how-to-handle-dependencies-of-imported-library-targets-with-target-runti>

> <https://stackoverflow.com/questions/14089284/copy-all-dlls-that-an-executable-links-to-to-the-executable-directory/73550650#73550650>

Also if you are using this feature I’d raise your CMake minimum required. Since it requires 3.21

```auto
cmake_minimum_required(VERSION 3.21)

```
