sdl2-cmake-scripts
sdl2-cmake-scripts copied to clipboard
Getting a linker error - probably my fault though
When my CMakeLists.txt is this
cmake_minimum_required(VERSION 3.4)
project(SDL-Tests)
file(GLOB all_src "main.c")
add_executable(sdl ${all_src})
set(CMAKE_C_COMPILER /usr/bin/gcc)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "#{PROJECT_SOURCE_DIR}/cmake")
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
target_link_libraries(sdl ${SDL2_LIBRARY})
I get the following linker error:
[ 50%] Linking C executable sdl
/usr/bin/cmake -E cmake_link_script CMakeFiles/sdl.dir/link.txt --verbose=1
/usr/bin/gcc CMakeFiles/sdl.dir/main.c.o -o sdl
CMakeFiles/sdl.dir/main.c.o: In function `main':
main.c:(.text+0x1e): undefined reference to `SDL_Init'
main.c:(.text+0x50): undefined reference to `SDL_CreateWindow'
main.c:(.text+0x60): undefined reference to `SDL_GetError'
main.c:(.text+0x85): undefined reference to `SDL_Delay'
main.c:(.text+0x91): undefined reference to `SDL_DestroyWindow'
main.c:(.text+0x96): undefined reference to `SDL_Quit'
collect2: error: ld returned 1 exit status
Whereas adding SDL2
to the target_link_libraries
makes the program link properly. I'm not great at this sort of thing - why is this happening? Any idea? Shouldn't ${SDL_LIBRARY}
be equivalent to SDL2
?
Any help would be much appreciated, but I assume it's me being stupid, as opposed to the cmake script not being correct.
I think that target_link_libraries must be used with ${SDL2_LIBRARIES}
, as it does not only contain ${SDL2_LIBRARY}
but also ${SDL2MAIN_LIBRARY}
Same problem on linux and windows.
# SDL2
find_package(SDL2 REQUIRED)
if ( SDL2_FOUND )
list (APPEND WSJCPP_INCLUDE_DIRS ${SDL2_INCLUDE_DIRS})
list (APPEND WSJCPP_LIBRARIES ${SDL2_LIBRARIES})
endif( SDL2_FOUND )
Error:
o: undefined reference to symbol 'SDL_FreeSurface'
changing SDL2_LIBRARIES
to SDL2_LIBRARY
- was help me