# CMAKE\_DL\_LIBS

**URL:** <https://discourse.cmake.org/t/cmake-dl-libs/1159>\
**Category:** Code\
**Created:** [May 7, 2020, 12:13am UTC](https://discourse.cmake.org/t/cmake-dl-libs/1159 "2020-05-07T00:13:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alex/32/125_2.png) [@alex](https://discourse.cmake.org/u/alex)\
**Post date:** [May 7, 2020, 12:13am UTC](https://discourse.cmake.org/t/cmake-dl-libs/1159/1 "2020-05-07T00:13:12Z")

</div>

What is the intended way of using CMAKE\_DL\_LIBS? Unlike most libraries in CMake, there doesn’t seem to be an imported target for it.

In particular, if I have a library that has an interface dependency on CMAKE\_DL\_LIBS on Unix-y platforms, but not on Windows, how should I export that target for install? With imported targets, I can `find_dependency` in my config and use a generator expression to selectively include the target. But this variable will be expanded at configure time which seems wrong (maybe a different unix has a different name for `-ldl` for some reason).

---

<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:** [May 7, 2020, 9:43am UTC](https://discourse.cmake.org/t/cmake-dl-libs/1159/2 "2020-05-07T09:43:57Z")

</div>

This variable is just a facility to manage system dynamic loader library in a portable way.

Because this is a system library, there is no need to export it. The variable definition depends on the target system (for example, on windows this variable is empty) so you can use it safely regardless the system you are targeting.

---

<div class="post-metadata">

**Author:** ![alex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alex/32/125_2.png) [@alex](https://discourse.cmake.org/u/alex)\
**Post date:** [May 8, 2020, 7:13am UTC](https://discourse.cmake.org/t/cmake-dl-libs/1159/3 "2020-05-08T07:13:00Z")

</div>

I know I can use it in my project safely. The issue is with _exporting_ a target which takes it as a dependency.

When I use an imported target, a `find_package` or `find_dependency` call on the client’s system takes care of filling in correct paths for, eg. `PNG::PNG`. There’s no genex along the lines of `$<DEFER_EXPAND:CMAKE_DL_LIBS>` which I could use to keep the expression `${CMAKE_DL_LIBS}` from expanding at the time I call `target_link_libraries`.

---

<div class="post-metadata">

**Author:** ![alex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alex/32/125_2.png) [@alex](https://discourse.cmake.org/u/alex)\
**Post date:** [May 8, 2020, 7:30am UTC](https://discourse.cmake.org/t/cmake-dl-libs/1159/4 "2020-05-08T07:30:16Z")

</div>

Here’s an example of what I mean:

```auto
cmake_minimum_required(VERSION 3.16)
project(Example)

find_package(PNG REQUIRED)

add_library(mylib main.cpp)
target_link_libraries(mylib PUBLIC PNG::PNG ${CMAKE_DL_LIBS})

include(GNUInstallDirs)
install(TARGETS mylib EXPORT Example_Targets
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
                COMPONENT Example_Runtime
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
                COMPONENT Example_Runtime
                NAMELINK_COMPONENT Example_Development
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
                COMPONENT Example_Development
        INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

install(EXPORT Example_Targets
        DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/Example"
        NAMESPACE Example::
        FILE Example-Targets.cmake
        COMPONENT Example_Development)

```

If I then build and install it to a local directory, the file `lib/cmake/Example/Example-Targets.cmake` contains the lines:

```auto
set_target_properties(Example::mylib PROPERTIES
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include"
  INTERFACE_LINK_LIBRARIES "PNG::PNG;dl"
)

```

This is clearly correct for `PNG::PNG`, but why should this be right for `dl`? I would have wanted to see (the equivalent of):

```auto
INTERFACE_LINK_LIBRARIES "PNG::PNG;${CMAKE_DL_LIBS}"

```

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [May 8, 2020, 8:16am UTC](https://discourse.cmake.org/t/cmake-dl-libs/1159/5 "2020-05-08T08:16:27Z")

</div>

As per Marc’s comments, the `dl` library is provided by the system. It’s okay to list such a library as a raw `dl` to `target_link_libraries()`, since even when exported/installed, that same library has to be provided by the target system too. I think you might be concerned about a non-problem in this case.

But in the interest of providing choices, if you really want to avoid `dl` being added to the installed properties, you could try wrapping `${CMAKE_DL_LIBS}` in a `$<BUILD_INTERFACE:...>`. That should prevent it from escaping your local build tree.
