如何在 CMake 中运行基本的`add_custom_command`

Ale*_*ney 5 cmake add-custom-command

我只是想启动并运行一个基本的 CMake 示例,该示例可以运行一些基本的命令行命令。我已经研究了一段时间,但我没有任何运气。我完全错误地使用了这个吗?任何和所有输入将不胜感激。

cmake_minimum_required(VERSION 3.0)

add_custom_command(
  OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/test.txt
  COMMAND echo "Hello world"
  COMMENT "This is a comment. But it's not printed during the build?"
)

return() 
Run Code Online (Sandbox Code Playgroud)

编辑: 使用:

add_custom_target(run ALL DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/test.txt)
Run Code Online (Sandbox Code Playgroud)

也不起作用,正如其他帖子中所建议的那样。