简体   繁体   中英

Using CMAKE find_package with SDL2

I've got a project that uses CMAKE, and needs to link SDL2. I'm trying to understand how find_package works. At one point in the past, I was able to get find_package to work by supplying my own FindSDL2.cmake, but after some new linker errors, I decided to try a different apporoach. The reading I'm doing seems to imply that, after I've installed the libsdl2-dev package, I should be able to just use find_package(SDL2 REQUIRED) and then target_link_libraries(Suqua PRIVATE SDL2::SDL2), but cmake throws a package not found error. Do I need to provide a custom FindSDL2.cmake?

CMakeLists.txt

    cmake_minimum_required (VERSION 3.8)

set(CMAKE_CXX_STANDARD 17)

find_package(SDL2 CONFIG REQUIRED)

file(GLOB source_files
    "src/*.cpp"
    "header/*.h"
)

add_library(Suqua ${source_files} "src/glad.c" )

target_include_directories(Suqua PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/header)
target_include_directories(Suqua PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../include)

target_link_libraries(Suqua PRIVATE SDL2::SDL2 enet)

if(UNIX)
    target_link_libraries(Suqua PRIVATE stdc++fs)
endif()

Error

CMake Error at Suqua/CMakeLists.txt:13 (add_library):
  Target "Suqua" links to target "SDL2::SDL2" but the target was not found.
  Perhaps a find_package() call is missing for an IMPORTED target, or an
  ALIAS target is missing?

Thank you, and if you have any other critiques of my CMakeLists, they'd be greatly appreciated!

Answer supplied by Tsyvarev

When using an installed library (not built from source), use the SD2_LIBRARIES variable.

On another note, I actually fixed this earlier, but assumed I was doing something wrong when I got a linker error relating to linking CMAKE_DL_LIBS, which I'd removed because I assumed it didn't do anything:/ Definitely gonna read through that CMake book. Thanks all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM