Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting a linker error - probably my fault though #20

Open
j4cobgarby opened this issue Mar 10, 2018 · 2 comments
Open

Getting a linker error - probably my fault though #20

j4cobgarby opened this issue Mar 10, 2018 · 2 comments

Comments

@j4cobgarby
Copy link

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
Copy link

Rocha57 commented Aug 7, 2019

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
Copy link

sea-kg commented May 3, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants