Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake generate error on windows as it uses \ as escape seq

Tags:

c++

windows

cmake

I have something such as this in my cmake:

set(MyLib_SRC $ENV{MyLib_DIR}/MyLib.cpp)
add_library(MyLibrary STATIC ${MyLib_SRC})

but when I ran the cmake, I am getting this error:

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.

I read this OS answer cmake parse error :Invalid escape sequence \o but How can I chang the macro (which macro!) to a function?

The value of env variable is

MyLib_DIR=D:\New\Development\Lib
like image 667
mans Avatar asked Jan 21 '15 15:01

mans


2 Answers

The problem is that $ENV{MyLib_DIR} expands the environment variable verbatim, including the backslashes used as path separators. These can then get re-interpreted as escape sequences.

What you want to do is convert the path to CMake's internal format before handling it in CMake code:

file(TO_CMAKE_PATH $ENV{MyLib_DIR} MyLib_DIR)

set(MyLib_SRC ${MyLib_DIR}/MyLib.cpp)
add_library(MyLibrary STATIC ${MyLib_SRC})
like image 68
Angew is no longer proud of SO Avatar answered Sep 17 '22 19:09

Angew is no longer proud of SO


I suppose cmake always expects UNIX-style path. So, what I do is

set(MayLib_PATH $ENV{MyLib_DIR})
string(REPLACE "\\" "/" MayLib_PATH "${MayLib_PATH}")
like image 28
Archie Avatar answered Sep 18 '22 19:09

Archie