# Copy DLL files to the binary directory on Mac and Linux.

**URL:** https://discourse.cmake.org/t/copy-dll-files-to-the-binary-directory-on-mac-and-linux/10508
**Category:** Code
**Tags:** os:linux, os:windows, os:macos
**Created:** [March 28, 2024, 2:11am UTC](https://discourse.cmake.org/t/copy-dll-files-to-the-binary-directory-on-mac-and-linux/10508 "2024-03-28T02:11:20Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Saviz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/saviz/32/6194_2.png) [@Saviz](https://discourse.cmake.org/u/Saviz)
#### Post date: [March 28, 2024, 2:11am UTC](https://discourse.cmake.org/t/copy-dll-files-to-the-binary-directory-on-mac-and-linux/10508/1 "2024-03-28T02:11:21Z")

</div>

According to the CMake documentation, we can use the following command to copy DLL files to the binary directory under the Windows operating system:

```auto
if(WIN32)
    
	add_custom_command(TARGET ${EXECUTABLE_NAME} POST_BUILD
		COMMAND ${CMAKE_COMMAND} -E copy_if_different
		$<TARGET_RUNTIME_DLLS:${EXECUTABLE_NAME}>
		$<TARGET_FILE_DIR:${EXECUTABLE_NAME}>
		COMMAND_EXPAND_LISTS
		COMMENT "Copying DLLs to target directory")
endif()

```

However, for MacOS and Linux operating systems, the process isn’t as straightforward. I was wondering if there is something similar in nature for them as well?

---

<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: [March 28, 2024, 11:29pm UTC](https://discourse.cmake.org/t/copy-dll-files-to-the-binary-directory-on-mac-and-linux/10508/2 "2024-03-28T23:29:50Z")

</div>

MacOS and Linux have RPATH so you don’t need to copy SHARED libraries into the same directory as your executable. Windows lacks RPATH functionality.

> **[rpath](https://en.wikipedia.org/wiki/Rpath)**
>
> In computing, rpath designates the run-time search path hard-coded in an executable file or library. Dynamic linking loaders use the rpath to find required libraries.
> Specifically, it encodes a path to shared libraries into the header of an executable (or another shared library). This RPATH header value (so named in the Executable and Linkable Format header standards) may either override or supplement the system default dynamic linking search paths.
> The rpath of an executable or shared library...

---

<div class="post-metadata">

### Author: ![Saviz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/saviz/32/6194_2.png) [@Saviz](https://discourse.cmake.org/u/Saviz)
#### Post date: [April 1, 2024, 5:02am UTC](https://discourse.cmake.org/t/copy-dll-files-to-the-binary-directory-on-mac-and-linux/10508/3 "2024-04-01T05:02:36Z")

</div>

> [@buildSystemPerson](#):
>
> MacOS and Linux have RPATH so you don’t need to copy SHARED libraries into the same directory as your executable.

Great, so I don’t need to copy anything. But, do I need to include any special command to instruct the system to take advantage of RPATH or are they just self aware?

---

<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: [April 1, 2024, 4:09pm UTC](https://discourse.cmake.org/t/copy-dll-files-to-the-binary-directory-on-mac-and-linux/10508/4 "2024-04-01T16:09:29Z")

</div>

IIRC by default RPATH will be used in the build tree when it’s available.
