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

前端 未结 2 420
不思量自难忘°
不思量自难忘° 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:25

    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}")
    
    0 讨论(0)
  • 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})
    
    0 讨论(0)
提交回复
热议问题