# Add interface library but I want to copy my .dll in the same CMake

**URL:** <https://discourse.cmake.org/t/add-interface-library-but-i-want-to-copy-my-dll-in-the-same-cmake/9319>\
**Category:** Code\
**Tags:** os:windows\
**Created:** [October 31, 2023, 9:49am UTC](https://discourse.cmake.org/t/add-interface-library-but-i-want-to-copy-my-dll-in-the-same-cmake/9319 "2023-10-31T09:49:59Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![ksdhans](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ksdhans/32/3964_2.png) [@ksdhans](https://discourse.cmake.org/u/ksdhans)\
**Post date:** [November 1, 2023, 3:25am UTC](https://discourse.cmake.org/t/add-interface-library-but-i-want-to-copy-my-dll-in-the-same-cmake/9319/2 "2023-11-01T03:25:09Z")

</div>

It looks like you’re trying to add a pre-compiled shared library. I suggest you use the IMPORTED keyword instead of INTERFACE. For example:

```
add_library(Foo SHARED IMPORTED)
set_target_properties(Foo PROPERTIES
    IMPORTED_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/bin/libfoo.dll"
    IMPORTED_IMPLIB "${CMAKE_CURRENT_SOURCE_DIR}/lib/libfoo.lib"/${CMAKE_STATIC_LIBRARY_PREFIX}SDL2${CMAKE_STATIC_LIBRARY_SUFFIX}
    IMPORTED_LOCATION_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/bin/libfoo.dll"
    IMPORTED_IMPLIB_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/lib/libfood.lib"
    INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/include"
)

```

The example above tells CMake to import a shared library, and gives it the location of all the files. You can then copy the DLL to the same directory as the executable by using the following:

```
# Copy DLL files on platforms where it's needed
if(CMAKE_IMPORT_LIBRARY_SUFFIX)
  add_custom_command(
    TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_RUNTIME_DLLS:${PROJECT_NAME}> $<TARGET_FILE_DIR:${PROJECT_NAME}>
    COMMAND_EXPAND_LISTS
  )
endif()

```

NOTE: It’s better to use ${CMAKE\_STATIC\_LIBRARY\_PREFIX}, {CMAKE\_STATIC\_LIBRARY\_SUFFIX}, and their shared library equivalents rather than hardcoding “lib” prefixes and “.dll” suffixes. I haven’t done it above to keep the example as simple as possible.

P.S., I cover how to link to pre-compiled libraries (and a lot more) in my CMake Tutorial book, which you can check out here: [https://cmaketutorial.com/](https://cmaketutorial.com/)

---

_[View the full topic](https://discourse.cmake.org/t/add-interface-library-but-i-want-to-copy-my-dll-in-the-same-cmake/9319)._
