cmake add_custom_target preserve directory

2019-08-30 07:59发布

问题:

Imagine the following lines in a CMakeFiles.txt:

add_custom_target( target
          cd bin
          COMMAND echo "test" > README
)

make target will not work as expected, as it will not modify the file bin/README but rather the file ./README. I found out that, in order to make CMake modify bin/README, I have to write

COMMAND cd bin && echo "test" > README

which is time consuming and blows the CMakeLists up when used multiple times. I want a behavior that is much like the behaviour of shell scripts. How can I achieve this?

回答1:

Use the WORKING_DIRECTORY directive:

add_custom_target( target
      COMMAND echo "test" > README
      WORKING_DIRECTORY bin
)

EDIT: Reversed COMMAND and WORKING_DIRECTORY order



标签: cmake