cmake:前导或尾随空格(策略CMP0004)

时间:2017-08-17 08:27:23

标签: c++ cmake sdl-2

我关注this question。但是我的cmake面临错误:

cmake

# CMakeLists.txt cmake_minimum_required(VERSION 3.5.1) project (main) add_executable(main main.cpp ) find_package(SDL2 REQUIRED) include_directories(${SDL2_INCLUDE_DIRS}) target_link_libraries(main ${SDL2_LIBRARIES}) 列表有什么问题?

我认为轻微的// main.cpp int main() { return 0; } 版本差异不会导致这样的错误。

/usr/lib/x86_64-linux-gnu/cmake/SDL2/sdl2-config.cmake

# sdl2 cmake project-config input for ./configure scripts

set(prefix "/usr") 
set(exec_prefix "${prefix}")
set(libdir "${prefix}/lib/x86_64-linux-gnu")
set(SDL2_PREFIX "/usr")
set(SDL2_EXEC_PREFIX "/usr")
set(SDL2_LIBDIR "${prefix}/lib/x86_64-linux-gnu")
set(SDL2_INCLUDE_DIRS "${prefix}/include/SDL2")
set(SDL2_LIBRARIES "-L${SDL2_LIBDIR}  -lSDL2 ")

更新

的内容 {{1}}

{{1}}

2 个答案:

答案 0 :(得分:5)

ar2015's answer是正确的,但无需修改sdl2-config.cmake

只需在target_link_libraries之前删除尾随空格:

string(STRIP ${SDL2_LIBRARIES} SDL2_LIBRARIES)

target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})

答案 1 :(得分:4)

解决方案是编辑sdl2-config.cmake文件。

您可以通过命令找到此文件:

apt-file search sdl2-config

在Ubuntu Ubuntu 16.04中,它位于

 /usr/lib/x86_64-linux-gnu/cmake/SDL2/sdl2-config.cmake

在源文件中,

# sdl2 cmake project-config input for ./configure scripts

set(prefix "/usr") 
set(exec_prefix "${prefix}")
set(libdir "${prefix}/lib/x86_64-linux-gnu")
set(SDL2_PREFIX "/usr")
set(SDL2_EXEC_PREFIX "/usr")
set(SDL2_LIBDIR "${prefix}/lib/x86_64-linux-gnu")
set(SDL2_INCLUDE_DIRS "${prefix}/include/SDL2")
set(SDL2_LIBRARIES "-L${SDL2_LIBDIR}  -lSDL2 ")   <---- here

在最后一行,还有一个额外的空间应该删除

BEFORE:    set(SDL2_LIBRARIES "-L${SDL2_LIBDIR}  -lSDL2 ")
AFTER :    set(SDL2_LIBRARIES "-L${SDL2_LIBDIR}  -lSDL2")

然后,问题就解决了。