CMake "Syntax error in cmake code ... Invalid character escape" in CI
By Kaveh Alemi·Latchkey
CMake treats backslash as an escape character inside quoted arguments. A literal Windows path like "C:\src" makes it report "Invalid character escape" and stop parsing the listfile.
What this error means
Configuration on a Windows runner fails with a syntax error pointing at a quoted path or string that contains a backslash CMake cannot interpret.
CMake
CMake Error at CMakeLists.txt:7:
Syntax error in cmake code at
C:/proj/CMakeLists.txt:7
when parsing string
C:\build\out
Invalid character escape '\b'.
Common causes
How to fix it
Use forward slashes in CMake paths
Replace backslashes with forward slashes, which CMake accepts on Windows.
Reconfigure and confirm the listfile parses.
CMakeLists.txt
# instead of "C:\build\out"
set(OUT_DIR "C:/build/out")
Normalize incoming paths
When a path arrives from the environment, normalize it with file(TO_CMAKE_PATH) so backslashes become slashes before use.
CMakeLists.txt
file(TO_CMAKE_PATH "$ENV{BUILD_DIR}" OUT_DIR)
How to prevent it
Always write paths with forward slashes in CMake code and run external paths through file(TO_CMAKE_PATH) so backslash escapes never reach the parser.
Frequently asked questions
What causes ""Invalid character escape""?
Configuration on a Windows runner fails with a syntax error pointing at a quoted path or string that contains a backslash CMake cannot interpret.