SDL: macOS: 'SDL2/SDL_main.h' file not found

Hi, I’m updating the dependencies of Unvanquished and tried to update from SDL 2.0.12 to 2.26.5.

The macOS CI runner reported me that build error:

In file included from /Users/runner/work/1/s/src/engine/client/cl_main.cpp:59:
/Users/runner/work/1/s/external_deps/macos-amd64-default_9/SDL2.framework/Headers/SDL.h:32:10: fatal error: 'SDL2/SDL_main.h' file not found
#include <SDL2/SDL_main.h>
         ^~~~~~~~~~~~~~~~~
1 error generated.

We use SDL2.framework files from the SDL2 dmg (SDL2-2.0.12.dmg or SDL2-2.26.5.dmg).

I noticed this change:

$ grep 'SDL_main.h' macos-amd64-default_8/SDL2.framework/Versions/A/Headers/SDL.h
#include "SDL_main.h"
$ grep 'SDL_main.h' macos-amd64-default_9/SDL2.framework/Versions/A/Headers/SDL.h
#include <SDL2/SDL_main.h>

It happens that the Headers/ folder in macOS SDL2.framework doesn’t contain any SDL2 folder so the SDL2/ prefix in the include lines break the includes.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 17 (4 by maintainers)

Most upvoted comments

The SDL2::SDL2 target will only link to the SDL2 library, not to SDL2_main. This is not done automatically because your project might use SDL_MAIN_HANDLED to avoid renaming of main to SDL_main.

Because yours does not, you need to add SDL2::SDL2main before SDL2::SDL2. The following will work on all platforms that provide SDL2_main, or not.

target_link_libraries(your_game PRIVATE $<$<TARGET_EXISTS:SDL2::SDL2main>:SDL2::SDL2main> SDL2::SDL2)

OK, I got the game engine building and linking against SDL2 on both macOS and Linux by doing that:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index b7b96f422..3853ede93 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -688,14 +688,12 @@ endif()
 
 # SDL, required for all targets on win32 because of iconv and SDL_SetHint(SDL_TIMER_RESOLUTION, 0)
 if (BUILD_CLIENT OR WIN32)
-    find_package(SDL2 REQUIRED)
-
-    include_directories(${SDL2_INCLUDE_DIR})
+    find_package(SDL2 REQUIRED CONFIG)
 
     if (WIN32)
-        set(LIBS_ENGINE_BASE ${LIBS_ENGINE_BASE} ${SDL2_LIBRARY})
+        set(LIBS_ENGINE_BASE ${LIBS_ENGINE_BASE} SDL2::SDL2)
     else()
-        set(LIBS_CLIENT ${LIBS_CLIENT} ${SDL2_LIBRARY})
+        set(LIBS_CLIENT ${LIBS_CLIENT} SDL2::SDL2)
     endif()
 
     mark_as_advanced(SDL2MAIN_LIBRARY SDL2_LIBRARY SDL2_INCLUDE_DIR)