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

前端 未结 2 419
不思量自难忘°
不思量自难忘° 2020-12-19 11:07

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 r

2条回答
  •  醉梦人生
    2020-12-19 11:40

    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})
    

提交回复
热议问题