tcbrindle / sdl2-cmake-scripts

CMake scripts for finding SDL2 headers and libraries on multiple platforms
326 stars 98 forks source link

Getting a linker error - probably my fault though #20

Open j4cobgarby opened 6 years ago

j4cobgarby commented 6 years ago

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.

Rocha57 commented 5 years ago

I think that target_link_libraries must be used with ${SDL2_LIBRARIES}, as it does not only contain ${SDL2_LIBRARY} but also ${SDL2MAIN_LIBRARY}

sea-kg commented 3 years ago

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