文章目录
- 1. 问题记录
- 2. 原因排查
- 3. 解决方案
1. 问题记录
-
我有两个同名文件test.txt,它们内容不同,但时间戳相同(文件属性中的修改时间相同)
-
我希望在cmake中利用install命令,将
${PATH_SRC}/test.txt
替换${PATH_DES}/tree/test.txt
install(FILES ${PATH_SRC}/test.txt DESTINATION ${PATH_DES}/tree)
-
结果无法成功替换
2. 原因排查
- 在cmake的规则里,对于同名文件,如果时间戳相同,则会忽略复制,所以替换不生效
3. 解决方案
-
将原始目录copy到
${PATH_DES}/tree
时,不要复制test.txt,再将新的test.txt复制到${PATH_DES}/tree
install(DIRECTORY ./tree DESTINATION ${PATH_DES}/tree PATTERN "test.txt" EXCLUDE) install(FILES ${PATH_SRC}/test.txt DESTINATION ${PATH_DES}/tree)
-
将
${PATH_DES}/tree
中原本的test.txt删除,再将新的test.txt复制到${PATH_DES}/tree
install(CODE "file(REMOVE ${PATH_DES}/tree/test.txt)") install(FILES ${PATH_SRC}/test.txt DESTINATION ${PATH_DES}/tree)
-
创建一个custom make 目标
add_custom_target(CopyTask COMMAND ${CMAKE_COMMAND} -E copy ${PATH_SRC}/test.txt ${PATH_DES}/tree/test.txt) # 运行上述CMakeList.txt文件 mkdir build && cd build cmake .. make CopyTask
-
附上:cmake官方文档