这里是重现问题的最小 CMakeLists.txt
:
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
set(BUILD_INFO_CPP "${CMAKE_BINARY_DIR}/build_info.cpp")
add_custom_target(GenerateBuildInfo ALL
COMMAND ${CMAKE_COMMAND} -E echo "#include Something" > "${BUILD_INFO_CPP}"
)
在每次构建时我都想重新生成${CMAKE_BINARY_DIR}/build_info.cpp
,
但是这段代码不会创建 build_info.cpp
,因为 cmake
执行它是这样的:
/usr/bin/cmake -E echo #include > /tmp/2/build/build_info.cpp
并且理解 shell 将 #
解释为注释的开始,所以这等同于
/usr/bin/cmake -E echo
我想知道 cmake
中没有实现特殊符号转义吗?
我使用 linux/bash 和 Makefile 生成器。
最佳答案
根据 add_custom_target
的官方文档(CMake 3.0):
If VERBATIM is given then all arguments to the commands will be escaped properly for the build tool so that the invoked command receives each argument unchanged. Note that one level of escapes is still used by the CMake language processor before add_custom_target even sees the arguments. Use of VERBATIM is recommended as it enables correct behavior. When VERBATIM is not given the behavior is platform specific because there is no protection of tool-specific special characters.
因此,添加 VERBATIM
以便命令在 add_custom_target
中正确转义。
这同样适用于所有版本及 CMake 2.8。
关于c++ - cmake add_custom_target 转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48311733/