1

Let's say I used find_package(SDL REQUIRED) now I need to link to this library through target_link_libraries what would be the name of the variable to pass to target_link_libraries? In general is there a way to find these variables other than heuristically guessing? I've tried SDL_LIBRARY, SDL2_LIBRARY, and SDL_LIBRARIES. I do have sdl2 installed.

I would've expected this to work but it did not:

cmake_minimum_required(VERSION 3.10)
project(someProject)

# Find SDL2
find_package(SDL REQUIRED)
find_package(SDL_image REQUIRED)

# Include directories
include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS})

# Set source files
set(SOURCES main.cpp)

# Set executable
add_executable(someProject ${SOURCES})

# Link libraries
target_link_libraries(someProject ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES})

I get

CMake Error at

/usr/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:230
(message):
   Could NOT find SDL (missing: SDL_LIBRARY SDL_INCLUDE_DIR)
Call Stack (most recent call first):
   /usr/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
   /usr/share/cmake/Modules/FindSDL.cmake:224 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
   CMakeLists.txt:5 (find_package)


-- Configuring incomplete, errors occurred!
2
  • this is really not a linux/UNIX-specific problem, but a programming problem, and would probably reach a much wider, and much more experienced with legacy CMake, audience on Stackoverflow.com Commented Apr 7 at 19:19
  • @Shelter: rolled back your edit. The idea was good, to format the error as code with the original line breaks and everything, but you made things worse by formatting the quoted markdown as code. Commented Apr 8 at 5:52

1 Answer 1

3

It's usually <MODULENAME>_LIBRARIES, and that's the case for SDL, as well.

But that#s just convention, you might need to read the respective find_<MODULENAME>.cmake script (often in places like /usr/share/cmake/Modules, /usr/lib/cmake/version/Modules or part of the source tree). Sadly, especially around hyphenation, capitalization and other naming nuances, it's wild west with these CMake modules. You either get good documentation from the library authors, or need to read the find_<MODULENAME>.cmake.

Also not that you should probably not use the legacy way of using libraries in CMake. Instead of your

# Find SDL2
find_package(SDL REQUIRED)
find_package(SDL_image REQUIRED)

# Include directories
include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS})

# Set source files
set(SOURCES main.cpp)

# Set executable
add_executable(someProject ${SOURCES})

# Link libraries
target_link_libraries(someProject ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES})

you should just use modern CMake, which makes things easy. If you only needed SDL2 (not SDL2_image), then your whole finding and using becomes this:

# Find SDL2, which has modern CMake bindings
find_package(SDL REQUIRED)

# Set executable
add_executable(someProject main.cpp)

# Link libraries (I assume you want PRIVATE here, `someProject` is not supposed to export SDL targets/symbols)
target_link_libraries(someProject PRIVATE SDL::SDL)

You don't have to set the include directories yourself, they are automatically set because you link your target against the target SDL::SDL exported by Find_SDL.cmake.

However, you need SDL2_image (are you sure? I thought that was part of SDL by now, but I might be mistaken), and that hasn't seen much love, so we need to do the modern CMake setup ourselves. Advantage is that we do that once, and from there on, we can always just use the target we set up once, and we don't have to fiddle with target_include_… stuff.

# Find SDL2, which has modern CMake bindings
find_package(SDL REQUIRED)

# SDL_image is legacy, so that's annoying, we have to fix things ourselves:
find_package(SDL_image REQUIRED)
if(NOT TARGET SDL::image)
  add_library(SDL::image INTERFACE IMPORTED)
  set_target_properties(SDL::image PROPERTIES
     INTERFACE_INCLUDE_DIRECTORIES "${SDL_IMAGE_INCLUDE_DIRS}"
     INTERFACE_LINK_LIBRARIES "${SDL_IMAGE_LIBRARIES}")
endif()

# Set executable
add_executable(someProject main.cpp)

# Link libraries 
target_link_libraries(someProject PRIVATE SDL::SDL SDL::image)

That's it; no need to remember multiple variable names, just the name of the targets you link against: SDL::SDL and our own SDL::image.

3
  • 1
    I've updated my code to what you suggest but I'm still getting the same exact error which has led me to believe that find_package doesn't find sdl. I've looked in /usr/share/cmake/Modules and I found a FindSDL.cmake file there this is the file that find_package is looking for right? And CMAKE_MODULE_PATH needs to point to that directory?
    – SGriffeth
    Commented Apr 8 at 0:58
  • well, yes, that's what the error message says: it couldn't find your SDL development installation. But your question was just about what the names of the variables are that hold the result when it can be found! Anyways, you need to make sure you've installed the -devel / -dev package for SDL, or however it's called on your Linux distro :) Commented Apr 8 at 5:51
  • Oh I see i had SDL installed but not the SDL development package which is what it was looking for
    – SGriffeth
    Commented Apr 8 at 11:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .