CMake file separator

In many cases, using the Unix-type slash file separator / will work even on Windows. Trying to manually specify backslash Windows file separator \ can cause problems in CMake and many other languages. Thus in CMake and other languages like Python, we always use / as path separator, and only transform to backslash for the rare cases it’s needed.

Transform to Unix file separator:

cmake_path(CONVERT "path\in\file.txt" TO_CMAKE_PATH_LIST out)

That switches backslash \ file separators to Unix slash / file separators. This becomes relevant if manually adjusting Include paths by appending to lib_INCLUDE_DIRS or similar. If backslashes sneak through, unexpected build-time errors can result, and even configure-time errors with “check_source_compiles()” and similar. As the docs note, put quotes "${mypath}" around the variable expansion to ensure CMake doesn’t mangle the path.

Transform to native file separator is generally more rarely used. CMake can transform paths to native file separator, with the caveat that this can cause unpredictable Windows-specific backslash problems, as with any program.


Related: CMake expanduser ~