Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake: leading or trailing whitespace (policy CMP0004)

Tags:

c++

cmake

sdl-2

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 ")
like image 502
ar2015 Avatar asked Aug 17 '17 08:08

ar2015


2 Answers

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})
like image 136
htiga Avatar answered Nov 09 '22 10:11

htiga


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.

like image 43
ar2015 Avatar answered Nov 09 '22 10:11

ar2015