Skip to content

Getting a linker error - probably my fault though #20

@j4cobgarby

Description

@j4cobgarby

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions