SDL2 and CMake: problem finding headers

Hello all,

I am using CMake to configure SDL2 include directories for my project. Configuration is done with this FindSDL2.cmake. Unfortunately with the latest version of SDL (2.14) I get an error: ‘SDL2/SDL_main.h’ file not found. I noticed, that ‘SDL2/’ was added to the include path in SDL.h. I don’t know how to fix this problem.

Can anyone help making this work again?

Hi,

Where did you download SDL2 from ? Currently, the latest version is 2.24.0 but if you’re using a fixed-point linux distribution, you may be behind (e.g. Ubuntu 22.04 has SDL 2.0.20, Debian Bullseye has SDL 2.0.14).

From what I’m seeing, even the stable debian package ships with the cmake config files and shouldn’t require you to use a custom FindSDL2. Could you try without it to see if it works ?

I’m very sorry! This was a typo. I meant the latest version 2.24.0. I am on macOS (also latest version).

No problem!

Yes, I have seen this issue around that using a 3rd party FindSDL2 package along the latest release breaks.

You should be able to just do this, without having to import any additional file:

add_executable(myExe <your sources>)

find_package(SDL2 CONFIG REQUIRED)

target_link_libraries(myExe SDL2::SDL2) # If you target a platform that needs SDL2main, add SDL2::SDL2main 

# Or
target_link_libraries(myExe ${SDL2_LIBRARIES})
target_include_directories(myExe ${SDL2_INCLUDE_DIRS})

Thank you very much! Unfortunately my CMake skills seem to be too bad to understand the solution. The files are here:
https://sourceforge.net/p/previous/code/HEAD/tree/trunk/CMakeLists.txt
https://sourceforge.net/p/previous/code/HEAD/tree/trunk/src/CMakeLists.txt
https://sourceforge.net/p/previous/code/HEAD/tree/trunk/cmake/FindSDL2.cmake

Maybe you can see the problem.

My problem still persists and I am unable to use SDL2 2.24.0. Does anyone have an idea how this can be fixed?

Creating a link inside the Headers directory (ln -s . ./SDL2) seems to fix the problem with finding the headers, but breaks code signature so that I still can’t use SDL2 v2.24.0.

Sorry for the late reply. I just got time to look deeper into it.

It’s a pretty complicated issue due to how Apple forces framework includes to prepend the name of the framework even if it doesn’t represent the real path on disk. In our case:

  • The real path is SDL2.framework/Headers/*.h
  • While Apple requires the include to be SDL2/*.h

CMake is usually smart about that and passes the framework with -isystem /Library/Frameworks/SDL2.framework to the compiler, which solves that issue.

When looking closer at your project, I noticed the following issues:

  • Pretty much all source files include main.h which in turn includes SDL.h
  • All the sub libraries (e.g. SoftFloat, Debug, Slirp) are not linking to SDL2 (even the one named GuiSdl oddly enough)

From manual testing, I have noticed that CMake will only emit an -isystem flag if the target links against SDL2, only setting the include directory will make it emit a -I flag that will fail to find the other SDL2 headers.

So here are the steps I took to get to compile using the framework release of SDL 2.24.0:

  • Delete FindSDL2.cmake, this is no longer needed and will conflict with the files shipped with SDL2 releases.
  • For every target created with add_library, follow with a target_link_libraries(My_Target PRIVATE SDL2::SDL2)

I also noticed here that you were using SDL2_LIBRARY, this is not provided by the official CMake configuration. You may want to use the target SDL2::SDL2 instead, it’s provided by config files even on the Debian stable SDL2 package!
There is also the SDL2_LIBRARIES variable, but be mindful that it may also contain SDL2::SDL2main when the target exists.

From testing, this change is also compatible when using Homebrew-packaged SDL2 if that’s a concern :stuck_out_tongue:

I hope this clears up the issue and how to solve it!

Hello Pierre,

thank you very much for the help! It seems this (https://sourceforge.net/p/previous/code/1161/) patch fixes my issues. Maybe I should check if really all libraries require SDL2 to work, but at least it compiles now.

I think this should also work on other platforms like Windows of Linux? I am not able to test. Would this also work with earlier versions of SDL2?

Best wishes,
Andreas