# CMake: How to set dependency from executable to custom command

**URL:** https://discourse.cmake.org/t/cmake-how-to-set-dependency-from-executable-to-custom-command/5623
**Category:** Usage
**Tags:** os:linux
**Created:** [May 6, 2022, 6:56am UTC](https://discourse.cmake.org/t/cmake-how-to-set-dependency-from-executable-to-custom-command/5623 "2022-05-06T06:56:10Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![swaraj\_somala](https://discourse.cmake.org/user_avatar/discourse.cmake.org/swaraj_somala/32/1668_2.png) [@swaraj\_somala](https://discourse.cmake.org/u/swaraj_somala)
#### Post date: [May 6, 2022, 6:56am UTC](https://discourse.cmake.org/t/cmake-how-to-set-dependency-from-executable-to-custom-command/5623/1 "2022-05-06T06:56:10Z")

</div>

I am trying to generate checksum for the executable using `add_custom_command`.

```
project(demo)
cmake_minimum_required(VERSION 3.12)
set(FILE_NAME Demo.cpp)
set(GEN_FILE_NAME ExeHashsha256)
set(EXE_NAME demoExe)
add_executable(${EXE_NAME} ${FILE_NAME})

add_custom_command(
    OUTPUT
        ${CMAKE_CURRENT_BINARY_DIR}/${GEN_FILE_NAME}
    COMMAND
        COMMAND ${CMAKE_COMMAND} -E sha256sum demoExe > ${CMAKE_CURRENT_BINARY_DIR}/${GEN_FILE_NAME}
    COMMENT
        "(Re)Running the custom command"
    DEPENDS
        ${CMAKE_CURRENT_BINARY_DIR}/${EXE_NAME}
)

```

I added the executable dependency for `add_custom_command` , however the checksum file is not being generated (ExeHashsha256). When I use `add_custom_target`, a checksum file is generated. As per my knowledge executable is one of the targets, thus the dependence can be included in ‘add custom command.’ Is it possible to generate without using `add_custom_target`?

---

<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 6, 2022, 7:37am UTC](https://discourse.cmake.org/t/cmake-how-to-set-dependency-from-executable-to-custom-command/5623/2 "2022-05-06T07:37:36Z")

</div>

CMake logic is based on targets, so no…

But you can attach a command to a target using `add_custom_command(TARGET <target> PRE_BUILD | PRE_LINK | POST_BUILD ...)`.
