# set output directory for your binaries

**URL:** <https://discourse.cmake.org/t/set-output-directory-for-your-binaries/1278>\
**Category:** Code\
**Created:** [May 27, 2020, 5:55pm UTC](https://discourse.cmake.org/t/set-output-directory-for-your-binaries/1278 "2020-05-27T17:55:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)\
**Post date:** [May 27, 2020, 5:55pm UTC](https://discourse.cmake.org/t/set-output-directory-for-your-binaries/1278/1 "2020-05-27T17:55:32Z")

</div>

I want to define the output path in which my binary is going to be created. It should reflect the behaviour of CMake build in functions. Variants which exist only for historical reasons are not being considered.

here is my solution:

```cmake
get_property( hasRunTimeDir TARGET ${targetName} PROPERTY RUNTIME_OUTPUT_DIRECTORY SET )
if( hasRunTimeDir )
	get_target_property( executableOutputPath ${targetName} RUNTIME_OUTPUT_DIRECTORY )
elseif( EXECUTABLE_OUTPUT_PATH )
	set( executableOutputPath ${EXECUTABLE_OUTPUT_PATH} )
else()
	set( executableOutputPath ${CMAKE_CURRENT_BINARY_DIR} )
endif()

```

this has been tested with GNU Makefile Generator.

---

<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:** [May 27, 2020, 6:25pm UTC](https://discourse.cmake.org/t/set-output-directory-for-your-binaries/1278/2 "2020-05-27T18:25:39Z")

</div>

Are you trying to change which directory the file gets built in, or get this information out of the target?

---

<div class="post-metadata">

**Author:** ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)\
**Post date:** [May 27, 2020, 6:30pm UTC](https://discourse.cmake.org/t/set-output-directory-for-your-binaries/1278/3 "2020-05-27T18:30:23Z")

</div>

the former. It is a generic target which should place the binary on the same location like a target of type executable would do.

---

<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:** [May 27, 2020, 6:43pm UTC](https://discourse.cmake.org/t/set-output-directory-for-your-binaries/1278/4 "2020-05-27T18:43:27Z")

</div>

Take a look at the [`CMAKE_RUNTIME_OUTPUT_DIRECTORY`](https://cmake.org/cmake/help/latest/variable/CMAKE_RUNTIME_OUTPUT_DIRECTORY.html) variable. Setting it will put the executable in the directory you want. You can also use [`CMAKE_LIBRARY_OUTPUT_DIRECTORY`](https://cmake.org/cmake/help/latest/variable/CMAKE_LIBRARY_OUTPUT_DIRECTORY.html) and [`CMAKE_ARCHIVE_OUTPUT_DIRECTORY`](https://cmake.org/cmake/help/latest/variable/CMAKE_ARCHIVE_OUTPUT_DIRECTORY.html).

---

<div class="post-metadata">

**Author:** ![hex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hex/32/363_2.png) [@hex](https://discourse.cmake.org/u/hex)\
**Post date:** [May 28, 2020, 7:29am UTC](https://discourse.cmake.org/t/set-output-directory-for-your-binaries/1278/5 "2020-05-28T07:29:08Z")

</div>

thanks. Using `CMAKE_RUNTIME_OUTPUT_DIRECTORY` directly is much better.

this is the corrected version:

```cmake
if( CMAKE_RUNTIME_OUTPUT_DIRECTORY )
	set( executableOutputPath ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} )
elseif( EXECUTABLE_OUTPUT_PATH )
	set( executableOutputPath ${EXECUTABLE_OUTPUT_PATH} )
else()
	set( executableOutputPath ${CMAKE_CURRENT_BINARY_DIR} )
endif()

```

I’ll look into libraries and archives when I get there.
