20

I am following this question. However my cmake faces with error:

-- Configuring done
CMake Error at CMakeLists.txt:18 (add_executable):
  Target "main" links to item "-L/usr/lib/x86_64-linux-gnu -lSDL2 " which has
  leading or trailing whitespace.  This is now an error according to policy
  CMP0004.


-- Generating done

What is wrong with the cmake list?

I do not think the slight cmake version difference leads to such an error.

# 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;
}

Update:

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

is

# 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 ")
ar2015
  • 5,558
  • 8
  • 53
  • 110
  • 2
    This looks like a problem in the SDL2-supplied package config file (but I don't use SDL2, so I can't comment more). – Angew is no longer proud of SO Aug 17 '17 at 08:45
  • 1
    There is should be `sdl2-config.cmake` file shipped with your SDL2 installation (it could be part of `libsdl2-dev` package or so). Show us content of this file. If it looks like this one: https://github.com/xerpi/SDL-Vita/blob/master/sdl2-config.cmake.in (content of *SD2_LIBRARIES* variable is enclosed into double quotes), then the error understandable. Fast fix could be removing these double quotes. – Tsyvarev Aug 17 '17 at 09:18
  • @Tsyvarev, updated. – ar2015 Aug 17 '17 at 09:55
  • @Tsyvarev, it is interesting. I converted the last line to `set(SDL2_LIBRARIES "-L${SDL2_LIBDIR} -lSDL2")` and now everything is fine. Thank you very much. I just wonder, why this problem has happened. Is it a bug in `CMake` or `SDL2`? – ar2015 Aug 17 '17 at 10:03
  • It is definitely problem with SDL2 config file. – Tsyvarev Aug 17 '17 at 14:21
  • You should add that as the answer, so thankful I found this so fast. Had to google the location too: /usr/lib/x86_64-linux-gnu/cmake/SDL2/sdl2-config.cmake. Simply removing that last space fixed it for me. – OrderAndChaos Feb 18 '18 at 21:51

2 Answers2

12

ar2015's answer is right, but its unnecessary to modify sdl2-config.cmake.

Just strip the trailing space before target_link_libraries:

string(STRIP ${SDL2_LIBRARIES} SDL2_LIBRARIES)

target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})
htiga
  • 261
  • 4
  • 6
7

The solution is to edit the sdl2-config.cmake file.

You can find this file via command:

apt-file search sdl2-config

In Ubuntu Ubuntu 16.04 it is located at

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

In the source file,

# 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

In the last line, there is an extra space which should be removed

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

Then, the problem fixed for me.

ar2015
  • 5,558
  • 8
  • 53
  • 110