# Get runtime library of my target

**URL:** <https://discourse.cmake.org/t/get-runtime-library-of-my-target/3563>\
**Category:** Code\
**Tags:** os:linux, os:windows, comp:gcc, comp:msvc\
**Created:** [June 13, 2021, 11:46pm UTC](https://discourse.cmake.org/t/get-runtime-library-of-my-target/3563 "2021-06-13T23:46:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [June 13, 2021, 11:46pm UTC](https://discourse.cmake.org/t/get-runtime-library-of-my-target/3563/1 "2021-06-13T23:46:04Z")

</div>

Hi,

I’m creating a library with `add_library(h5geo SHARED ${src_files_h5geo} ${include_files_h5geo})`.  
Then I need to get path to `h5geo` runtime library (for now I’m on Windows so I would like to get path to `h5geo.dll`)

I’m trying to do that with commands:

```cmake
add_library(h5geo SHARED ${src_files_h5geo} ${include_files_h5geo})

get_target_property(rname h5geo RUNTIME_OUTPUT_NAME)
get_target_property(rdir h5geo RUNTIME_OUTPUT_DIRECTORY)
message("rname: ${rname}") # rname: rname-NOTFOUND
message("rdir: ${rdir}") # rdir: rdir-NOTFOUND

```

but this prints `NOTFOUND`.

What is the correct way to do this?

---

<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:** [June 14, 2021, 10:10am UTC](https://discourse.cmake.org/t/get-runtime-library-of-my-target/3563/2 "2021-06-14T10:10:37Z")

</div>

During configuration step, these properties are **input** properties. So only usable **if** you have already set some value.

The final information is only available during generation step. For that purpose, you should use [generator expressions](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html) like `$<TARGET_PROPERTY:h5geo,RUNTIME_OUTPUT_NAME>`.

---

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [June 14, 2021, 4:15pm UTC](https://discourse.cmake.org/t/get-runtime-library-of-my-target/3563/3 "2021-06-14T16:15:28Z")

</div>

Thank you for answer

Can’t understand how exactly should I use.

For example can I use it during install step? I have:

```cmake
install(CODE [[
  file(GET_RUNTIME_DEPENDENCIES
    LIBRARIES $<TARGET_FILE:_h5geo>
    RESOLVED_DEPENDENCIES_VAR _r_deps
    UNRESOLVED_DEPENDENCIES_VAR _u_deps
    CONFLICTING_DEPENDENCIES_PREFIX conflict
    DIRECTORIES $<TARGET_PROPERTY:h5geo,RUNTIME_OUTPUT_DIRECTORY>
    PRE_INCLUDE_REGEXES "^.*([Hh][Dd][Ff][5]|[Hh][5][Gg][Ee][Oo]).*$"
    PRE_EXCLUDE_REGEXES "[^\n]+"
  )
  foreach(_file ${_r_deps})
    message(STATUS "Dependency R: ${_file}")
  endforeach()

  foreach(_file ${_u_deps})
    message(STATUS "Dependency U: ${_file}")
  endforeach()

  foreach(_file ${conflict_FILENAMES})
    message(STATUS "Conflict: ${_file}")
  endforeach()

  message("RUNTIME_OUTPUT_DIRECTORY: $<TARGET_PROPERTY:h5geo,RUNTIME_OUTPUT_DIRECTORY>") # shows empty
]])

```

As you can see I’m trying to add `$<TARGET_PROPERTY:h5geo,RUNTIME_OUTPUT_DIRECTORY>` runtime dir to add directory to search path.

---

<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:** [June 14, 2021, 4:54pm UTC](https://discourse.cmake.org/t/get-runtime-library-of-my-target/3563/4 "2021-06-14T16:54:06Z")

</div>

Generator expressions are not usable everywhere, only when explicitly specified in the documentation of the commands. For example, `install(CODE)` does not support generator expressions.

In your case, the most simple way to achieve what you want is to generate a script using [file(GENERATE)](https://cmake.org/cmake/help/latest/command/file.html#generate) command and execute it using [install(SCRIPT)](https://cmake.org/cmake/help/latest/command/install.html#code) command.

---

<div class="post-metadata">

**Author:** ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)\
**Post date:** [June 14, 2021, 5:22pm UTC](https://discourse.cmake.org/t/get-runtime-library-of-my-target/3563/5 "2021-06-14T17:22:04Z")

</div>

> [@marc.chevrier](#):
>
> For example, `install(CODE)` does not support generator expressions.

As of CMake 3.14, `install(CODE)` supports generator expressions. See policy [CMP0087](https://cmake.org/cmake/help/latest/policy/CMP0087.html).

---

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [June 14, 2021, 5:35pm UTC](https://discourse.cmake.org/t/get-runtime-library-of-my-target/3563/6 "2021-06-14T17:35:13Z")

</div>

Thank you for hint  
Now I set `cmake_policy(SET CMP0087 NEW)` but I get empty when trying to cmake install the code below:

```cmake
cmake_policy(SET CMP0087 NEW)

install(CODE "message(\"RUNTIME_OUTPUT_DIRECTORY: $<TARGET_PROPERTY:h5geo,RUNTIME_OUTPUT_DIRECTORY>\")")

```

Is it necessary to **set** `RUNTIME_OUTPUT_DIRECTORY` target property before reading it? Or there is a default path (MSVC stores binaries at `Release/Config` folder inside buinding tree for example) that I can get using generator expression?

---

<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:** [June 14, 2021, 5:43pm UTC](https://discourse.cmake.org/t/get-runtime-library-of-my-target/3563/7 "2021-06-14T17:43:46Z")

</div>

@kyle.edwards You are right, I read the documentation too quickly.

---

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [June 14, 2021, 6:51pm UTC](https://discourse.cmake.org/t/get-runtime-library-of-my-target/3563/8 "2021-06-14T18:51:01Z")

</div>

Cna’t understand. `install(CODE "message(\"TARGET_FILE: $<TARGET_FILE:h5geo>\")")` shows my library.  
But if I put the same code to the script and call it `install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/h5geopy-install.cmake)` then it will literally print `TARGET_FILE: $<TARGET_FILE:h5geo>`

---

<div class="post-metadata">

**Author:** ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)\
**Post date:** [June 14, 2021, 7:15pm UTC](https://discourse.cmake.org/t/get-runtime-library-of-my-target/3563/9 "2021-06-14T19:15:37Z")

</div>

`install(SCRIPT)` does not evaluate generator expressions contained in the `SCRIPT` itself, only the filename passed to `install(SCRIPT)`:

```cmake
install(SCRIPT $<TARGET_FILE_DIR:tgt>/script.cmake)

```

If you want the contents of the script file to contain generator expressions, you have to use `file(GENERATE)` first as per @marc.chevrier’s suggestion.

---

<div class="post-metadata">

**Author:** ![kerim](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/6f9a4e/32.png) [@kerim](https://discourse.cmake.org/u/kerim)\
**Post date:** [June 15, 2021, 12:52am UTC](https://discourse.cmake.org/t/get-runtime-library-of-my-target/3563/10 "2021-06-15T00:52:36Z")

</div>

With your help I could solve my problem

1. I get runtime with command `$<TARGET_FILE:h5geo>`
2. generator expression is used inside `file(GENERATE ...)`:

```cmake
configure_file(${CMAKE_SOURCE_DIR}/cmake/h5geopy-install.cmake.in
  ${CMAKE_CURRENT_BINARY_DIR}/h5geopy-install.cmake
  @ONLY
  )

# $<CONFIG> at the end of filename is necessary for multiconfig generators
# such as MSVC or Ninja multiconfig (gives error without it:
# "Evaluation file to be written multiple times with different content."
file(GENERATE
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/h5geopy-install-$<CONFIG>.cmake
  INPUT ${CMAKE_CURRENT_BINARY_DIR}/h5geopy-install.cmake
  )

```

So for the first time I simply configure file and set marked with `@` variables and after that I use `file(GENERATE ...)` where my `$<TARGET_FILE:h5geo>` transforms to path.

Thank you a lot!
