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'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})
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With