c++ - cmake add_custom_target 转义

标签 c++ c cmake escaping

这里是重现问题的最小 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/

相关文章:

c++ - 为什么我不能将这个从我的 BST 获取的节点推送到这个堆栈中?

c++ - CMake:为#cmakedefine 变量设置不同的名称

cmake - 符号链接(symbolic link) CMake

c++ - 确定多个集合中的唯一值

c++ - 如何在 Linux 上为 C++ 应用程序分配 "huge"页面

从 C 调用 Swift

c - "Bad permissions for mapped region at address"哈希表的 Valgrind 错误

c - 内核模块使文件错误

c++ - 如何配置在 CLion 中构建项目的方式?

c++ - Xcode 4.6/Clang 4.2 找不到 C++11 header