转自:https://blog.csdn.net/qq_38410730/article/details/102797448
1. add_custom_target
它用来创建target,用于make。 例子:
cmake_minimum_required(VERSION 3.0) project(test) add_custom_target(CopyTask COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/config ${CMAKE_CURRENT_SOURCE_DIR}/etc COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/log.txt ${CMAKE_CURRENT_SOURCE_DIR}/etc )
- target是后面的命令的目标。
- command就是生成目标文件的命令,一般就是编译命令。
即make target相当于执行后面的所有命令。
上面的命令为将config目录下的内容拷贝到etc目录下,将log.txt文件拷贝到etc目录下。${CMAKE_COMMAND}是CMake的路径,也就是cmake工具的目录,-E表示CMake运行命令,copy_directory和copy是cmake_command_line,再后面两个就是command_line的参数。
运行上述CMakeList.txt文件:
mkdir build && cd build cmake .. make CopyTask
初始目录结构:
|---build |---config |---log.log |---CMakeLists.txt |---log.txt
结果:
|---build |---config |---log.log |---etc |---log.log |---log.txt |---CMakeLists.txt |---log.txt