# How to put a result file to any subdirectory

**URL:** https://discourse.cmake.org/t/how-to-put-a-result-file-to-any-subdirectory/3266
**Category:** Usage
**Tags:** os:windows, gen:makefiles
**Created:** [May 5, 2021, 8:40am UTC](https://discourse.cmake.org/t/how-to-put-a-result-file-to-any-subdirectory/3266 "2021-05-05T08:40:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![polarisru](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/958977/32.png) [@polarisru](https://discourse.cmake.org/u/polarisru)
#### Post date: [May 5, 2021, 8:40am UTC](https://discourse.cmake.org/t/how-to-put-a-result-file-to-any-subdirectory/3266/1 "2021-05-05T08:40:06Z")

</div>

I try to compile my Cortex-M0 project with CMake and arm-none-eabi-gcc (mingw32-make as make).  
Everything is Ok, I get my hex and binary as result, but I want to have them not in the main directory with CMakeLists.txt, but in the separate directory, “bin” for example.  
I’ve found, that CMAKE\_RUNTIME\_OUTPUT\_DIRECTORY must do the thing, but it doesn’t work properly.  
I try to define it within my CMakeLists.txt:

> set(CMAKE\_RUNTIME\_OUTPUT\_DIRECTORY bin)

But I get this error during the linking:

> [100%] Linking C executable bin\xxx.out  
> arm-none-eabi-size: ‘xxx.out’: No such file  
> mingw32-make[2]: \*\*\* [CMakeFiles\xxx.out.dir\build.make:913: bin/xxx.out] Error 1  
> mingw32-make[2]: \*\*\* Deleting file ‘bin/xxx.out’  
> mingw32-make[1]: \*\*\* [CMakeFiles\Makefile2:82: CMakeFiles/xxx.out.dir/all] Error 2  
> mingw32-make: \*\*\* [Makefile:90: all] Error 2

Bin directory was created, but it’s empty, so probably xxx.out was not created properly. How could I get it working?  
If I remove CMAKE\_RUNTIME\_OUTPUT\_DIRECTORY definition, everything is Ok, linking is successful, but all my files (xxx.out, xxx.bin, xxx.hex) are placed to a root directory.

Here is the whole content of my CMakeLists.txt:

> cmake\_minimum\_required(VERSION 3.5)  
> project(xxx)
> 
> enable\_language(C ASM)  
> set(CMAKE\_C\_STANDARD 99)  
> set(CMAKE\_C\_STANDARD\_REQUIRED ON)  
> set(CMAKE\_C\_EXTENSIONS OFF)
> 
> set(EXECUTABLE ${PROJECT\_NAME}.out)
> 
> #set(CMAKE\_RUNTIME\_OUTPUT\_DIRECTORY bin)
> 
> include\_directories(src)  
> set(sources  
> src/main.c  
> )  
> add\_executable (${EXECUTABLE} ${sources})
> 
> target\_compile\_definitions(${EXECUTABLE} PRIVATE  
> -D\_\_ATSAMC21E18A\_\_  
> -D\_\_TARGET\_CPU\_CORTEX\_M0  
> )
> 
> target\_compile\_options(${EXECUTABLE} PRIVATE  
> -mcpu=cortex-m0plus  
> -march=armv6-m  
> -mthumb  
> -mfloat-abi=soft  
> -fdata-sections  
> -ffunction-sections  
> -fno-strict-aliasing  
> -Wall  
> -O2  
> )
> 
> target\_link\_options(${EXECUTABLE} PRIVATE  
> -Tsys/samc21g18a\_release.ld  
> -mcpu=cortex-m0plus  
> -march=armv6-m  
> -mthumb  
> -mfloat-abi=soft  
> -Wl,-Map=${PROJECT\_NAME}.map,–cref  
> -specs=nano.specs  
> -Wl,–gc-sections  
> -lm  
> -lc  
> -lnosys  
> )
> 
> target\_link\_libraries(${EXECUTABLE} PRIVATE  
> m  
> )
> 
> add\_custom\_command(TARGET ${EXECUTABLE}  
> POST\_BUILD  
> COMMAND arm-none-eabi-size ${EXECUTABLE})
> 
> add\_custom\_command(TARGET ${EXECUTABLE}  
> POST\_BUILD  
> COMMAND arm-none-eabi-objcopy -O ihex ${EXECUTABLE} ${PROJECT\_NAME}.hex  
> COMMAND arm-none-eabi-objcopy -O binary ${EXECUTABLE} ${PROJECT\_NAME}.bin)

Do you have any ideas? Thank you in advance!

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 5, 2021, 10:18am UTC](https://discourse.cmake.org/t/how-to-put-a-result-file-to-any-subdirectory/3266/2 "2021-05-05T10:18:52Z")

</div>

The problem is in your `POST_BUILD` commands. They used to work because, by default the working directory is `CMAKE_CURRENT_BINARY_DIR`. Now that the binary has moved to another directory, they cannot “find” the executable by just its name. Instead of passing `${EXECUTABLE}` as an argument, I would recommend `$<TARGET_FILE:${EXECUTABLE}>` instead. This will also work reliably for multi-config generators.

---

<div class="post-metadata">

### Author: ![polarisru](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/958977/32.png) [@polarisru](https://discourse.cmake.org/u/polarisru)
#### Post date: [May 5, 2021, 1:57pm UTC](https://discourse.cmake.org/t/how-to-put-a-result-file-to-any-subdirectory/3266/3 "2021-05-05T13:57:07Z")

</div>

Thank you for your reply!  
Probably you are right, the configuration was not Ok, but I’ve found another problem with this solution - the whole building process will be done in the project’s root directory, but I prefer to have everything in a separate directory. My previous workout was like that:

> cmake -G “MinGW Makefiles” -DCMAKE\_TOOLCHAIN\_FILE=arm-none-eabi-gcc.cmake  
> mingw32-make

But now I define the build directory in the command line and perform the build process with cmake tools only:

> cmake -G “MinGW Makefiles” -DCMAKE\_TOOLCHAIN\_FILE=arm-none-eabi-gcc.cmake . -Bbin  
> cmake --build ./bin

The only problem is a lot of temporary files I wish to delete after the build process. But this is another issue.

Now everything related to the build process is in the **bin** directory including the hex and bin files.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 5, 2021, 6:02pm UTC](https://discourse.cmake.org/t/how-to-put-a-result-file-to-any-subdirectory/3266/4 "2021-05-05T18:02:23Z")

</div>

> [@polarisru](#):
>
> The only problem is a lot of temporary files I wish to delete after the build process. But this is another issue.

Such as? And why are they a problem. If you want it for packaging or other purposes, it is usually far better to use installation to pull things out of the build tree for use elsewhere.

> [@polarisru](#):
>
> Now everything related to the build process is in the **bin** directory including the hex and bin files.

Maybe I’m confused. This is what you wanted right?

It seems you were using in-source builds and wanted to move the binaries? I would recommend using out-of-source builds (as `-Bbin` will do) and then use installation to “remove” the temporary files.
