Question: I'm trying to generate a header with a custom command. The header should be updated on each rebuild, so that the source file which includes it would also be rebuilt. (Actual command is a script, but here is a simplified version.)

Here's my project:

> 
> CMakeLists.txt
> 
> 
> 

```
cmake_minimum_required(VERSION 2.8)
set(test_SOURCES test.c)
include_directories("${CMAKE_BINARY_DIR}")

set(VERSION_H_PATH "${CMAKE_BINARY_DIR}/version.h")
message("VERSION_H_PATH: ${VERSION_H_PATH}")
add_custom_command(OUTPUT "${VERSION_H_PATH}" COMMAND "touch" "${VERSION_H_PATH}")
#add_custom_target(GENERATE COMMAND "touch" "${VERSION_H_PATH}")
add_executable(myprog ${test_SOURCES})
add_dependencies(myprog GENERATE)

```

> 
> test.c
> 
> 
> 

```
#include <version.h>

int main()
{
    return 0;
}

```

Now the problem is that the `CMakeList.txt`, as presented above, doesn't result in `version.h` being created at all. Only after I switch from `add_custom_target` to `add_custom_command` does it do. But then, if I change the file somehow, next `make` doesn't rebuild the project.

Looks like CMake doesn't recognize that `test.c` depends on `version.h`, although it does explicitly `#include` it. What am I doing wrong here?

Answer: 
OK, actually the question was badly formulated from the beginning. I shouldn't have simplified the generating command as much as I did. But the problems I had seem to have been solved by **removing content of build directory and re-running `cmake`**. For some reason some changes to `CMakeLists.txt` are not easy for cmake to understand. So here's the `CMakeLists.txt` which does work, and with not too much simplified generator command:

```
cmake_minimum_required(VERSION 2.8)
set(test_SOURCES test.c)
include_directories("${CMAKE_BINARY_DIR}")

set(VERSION_H_PATH "${CMAKE_BINARY_DIR}/version.h")
message("VERSION_H_PATH: ${VERSION_H_PATH}")
add_custom_target(GENERATE COMMAND "bash" "-c" "[ -e ${VERSION_H_PATH} ] \\|\\| touch ${VERSION_H_PATH}")
add_executable(myprog ${test_SOURCES})
add_dependencies(myprog GENERATE)

```

This appears to work even when using the reserved `test` target instead of `myprog`.
Every custom command needs a driver (something that will depend on its output). In your case, the best driver would be the `test` executable, which means you should add the generated file into your executable's sources:

```
add_executable(test ${test_SOURCES} ${VERSION_H_PATH})

```