cmake 在 windows 上生成错误,因为它使用 \ 作为转义序列
cmake generate error on windows as it uses \ as escape seq
我的 cmake 中有这样的东西:
set(MyLib_SRC $ENV{MyLib_DIR}/MyLib.cpp)
add_library(MyLibrary STATIC ${MyLib_SRC})
但是当我 运行 cmake 时,出现此错误:
CMake Warning (dev) at CMakeLists.txt:97 (add_library):
Syntax error in cmake code when parsing string
D:\New\Development\Lib/myLib.cpp
Invalid escape sequence \N
Policy CMP0010 is not set: Bad variable reference syntax is an error. Run
"cmake --help-policy CMP0010" for policy details. Use the cmake_policy
command to set the policy and suppress this warning.
This warning is for project developers. Use -Wno-dev to suppress it.
我阅读了这个 OS 答案 cmake parse error :Invalid escape sequence \o 但是我如何将宏(哪个宏!)更改为函数?
环境变量的值为
MyLib_DIR=D:\New\Development\Lib
我想 cmake 总是需要 UNIX 风格的路径。
所以,我做的是
set(MayLib_PATH $ENV{MyLib_DIR})
string(REPLACE "\" "/" MayLib_PATH "${MayLib_PATH}")
问题是 $ENV{MyLib_DIR}
逐字扩展了环境变量,包括用作路径分隔符的反斜杠。然后可以将这些重新解释为转义序列。
您要做的是在 CMake 代码中处理之前将路径转换为 CMake 的内部格式:
file(TO_CMAKE_PATH $ENV{MyLib_DIR} MyLib_DIR)
set(MyLib_SRC ${MyLib_DIR}/MyLib.cpp)
add_library(MyLibrary STATIC ${MyLib_SRC})
如 Angew 的回答所示,环境变量中的反斜杠被解释为转义序列。从 CMake 3.20 开始,cmake_path
command. Using the SET
选项提供了许多路径操作工具,此命令会将输入转换为带有正斜杠 (/
) 的 CMake 路径:
cmake_path(SET MyLib_SRC $ENV{MyLib_DIR}/MyLib.cpp)
add_library(MyLibrary STATIC ${MyLib_SRC})
MyLib_SRC
变量现在将包含正斜杠:
D:/New/Development/Lib/MyLib.cpp
我的 cmake 中有这样的东西:
set(MyLib_SRC $ENV{MyLib_DIR}/MyLib.cpp)
add_library(MyLibrary STATIC ${MyLib_SRC})
但是当我 运行 cmake 时,出现此错误:
CMake Warning (dev) at CMakeLists.txt:97 (add_library):
Syntax error in cmake code when parsing string
D:\New\Development\Lib/myLib.cpp
Invalid escape sequence \N
Policy CMP0010 is not set: Bad variable reference syntax is an error. Run
"cmake --help-policy CMP0010" for policy details. Use the cmake_policy
command to set the policy and suppress this warning.
This warning is for project developers. Use -Wno-dev to suppress it.
我阅读了这个 OS 答案 cmake parse error :Invalid escape sequence \o 但是我如何将宏(哪个宏!)更改为函数?
环境变量的值为
MyLib_DIR=D:\New\Development\Lib
我想 cmake 总是需要 UNIX 风格的路径。 所以,我做的是
set(MayLib_PATH $ENV{MyLib_DIR})
string(REPLACE "\" "/" MayLib_PATH "${MayLib_PATH}")
问题是 $ENV{MyLib_DIR}
逐字扩展了环境变量,包括用作路径分隔符的反斜杠。然后可以将这些重新解释为转义序列。
您要做的是在 CMake 代码中处理之前将路径转换为 CMake 的内部格式:
file(TO_CMAKE_PATH $ENV{MyLib_DIR} MyLib_DIR)
set(MyLib_SRC ${MyLib_DIR}/MyLib.cpp)
add_library(MyLibrary STATIC ${MyLib_SRC})
如 Angew 的回答所示,环境变量中的反斜杠被解释为转义序列。从 CMake 3.20 开始,cmake_path
command. Using the SET
选项提供了许多路径操作工具,此命令会将输入转换为带有正斜杠 (/
) 的 CMake 路径:
cmake_path(SET MyLib_SRC $ENV{MyLib_DIR}/MyLib.cpp)
add_library(MyLibrary STATIC ${MyLib_SRC})
MyLib_SRC
变量现在将包含正斜杠:
D:/New/Development/Lib/MyLib.cpp