14

I use SDL2 in my game. I always was using custom FindSDL2.cmake, because there is no one in standard CMake set. However, some time ago posts about FindSDL2 did appeared. Example: Reddit post.

If your cmake is new enough and it has FindSDL2.cmake file, you can do this:

find_package(SDL2 REQUIRED)

But when I download latest CMake (3.13.2), it doesn't include FindSDL2.cmake.

What happened to it?

1
  • 1
    Can you show use your CMakeLists.txt? FindSDL2 is found in cmake 3.12.1 at least. Jan 2, 2019 at 12:11

2 Answers 2

12

FindSDL2 has never appeared in CMake. Following the reject reason in pull request #149, SDL2 ships with a SDL2Config.cmake, which provides a cmake package. The documentation for find_package states that find_package(SDL2) will behave as follows:

  • Look for FindSDL2.cmake, use that if it exists. (module mode)
  • Otherwise, use the information in SDL2Config.cmake or sdl2-config.cmake. (config mode)

In short, make sure that your SDL2 package has installed the SDL2Config.cmake file and that is on your CMAKE_PREFIX_PATH. The documentation lists the exact paths and prefixes it looks under.

Once you managed to find_package(SDL2), the following targets are available for use in target_link_libraries:

SDL2::SDL2main
SDL2::SDL2
SDL2::SDL2-static
2
  • In my Ubuntu I can only find /usr/lib/x86_64-linux-gnu/cmake/SDL2/sdl2-config.cmake. Jan 2, 2019 at 12:38
  • From the documentation: Given the name Foo, it looks for a file called FooConfig.cmake or foo-config.cmake. I added the second form to the answer as well.
    – Botje
    Jan 2, 2019 at 13:12
6

To easily integrate the SDL2 library, I developed cross-platform modern CMake modules for finding and using the SDL2 library as well as other related libraries:

So the only things that you should do in order to integrate the SDL2 library are:

  1. Clone SDL2 CMake modules inside your project:
git clone https://github.com/aminosbh/sdl2-cmake-modules cmake/sdl2
  1. Add the following lines in your main CMakeLists.txt
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2)
find_package(SDL2 REQUIRED)
target_link_libraries(${PROJECT_NAME} SDL2::Main)

Note: If CMake didn't find the SDL2 library (in Windows), we can specify the CMake option SDL2_PATH as follows:

cmake .. -DSDL2_PATH="/path/to/sdl2"

For more details, please read the README.md file.
This is a list of SDL2 samples and projects: https://github.com/aminosbh/sdl-samples-and-projects

2
  • Apologises for asking here but your github and stackoverflow do not have PMs to message directly. Having issues setting the path for SDL2 ` -DSDL2_PATH=C:\Library\SDL2 -- Could NOT find . (missing: SDL2_LIBRARY) (found version "2.0.12") -- Found SDL2main: C:/Library/SDL2/lib/libSDL2main.a (found version "2.0.12") ` May 10, 2020 at 12:11
  • The repo is updated 4 years ago, do they still work with latest SDL2 version?
    – Jeff
    Apr 22 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.