Including SDL2 as a Framework (macOS) help

I have installed the latest version of SDL2 as a framework (downloaded from the sdl2 website). So it appears alongside other frameworks (/Library/Frameworks/SDL2.framework).

I have been following the “Linking Frameworks” chapter from Professional CMake and cannot seem to make it work.

I have the following:

find_library(SDL2Fwk SDL2 REQUIRED)
message(STATUS "found SDL2Fwk=${SDL2Fwk}")
# trying to build imgui library
add_library(imgui STATIC "${imgui_BUILD_SOURCES}")
target_include_directories(imgui PUBLIC "${imgui_CPP_SRC_DIR}")
target_link_libraries(imgui PUBLIC "-framework SDL2")

which generates

-- found SDL2Fwk=/Library/Frameworks/SDL2.framework

So clearly the framework is found in its proper location. But that does not seem to work as I get:

fatal error: 'SDL.h' file not found
#include <SDL.h>
1 error generated.

I have tried with ninja as a generator (which is the default with CLion) but also the XCode generator.

Any help would be appreciated.


Note that if I change to this:

target_include_directories(imgui PUBLIC "${imgui_CPP_SRC_DIR}" "${SDL2Fwk}/Headers")

thus explicitly adding the location of the headers, the code compiles fine, but the linking fails:

: && /Applications/ -g -isysroot /Applications/ -mmacosx-version-min=10.14 -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/re-edit.dir/src/cpp/re/edit/Application.cpp.o CMakeFiles/re-edit.dir/src/cpp/re/edit/macos/ -o re-edit  external/ocornut/imgui/libimgui.a  -framework SDL2 && :
ld: framework not found SDL2

Use instead:

target_link_libraries(imgui PUBLIC "${SDL2Fwk}")

And you will get correct compile and link flags for the framework (not need to use tagret_include_directories).

@marc.chevrier Unfortunately your suggestion does not work. Here is what I am getting:

FAILED: external/ocornut/imgui/CMakeFiles/imgui.dir/backends/imgui_impl_sdl.cpp.o
-I/Volumes/Development/github/org.pongasoft/re-edit/external/ocornut/imgui -g 
-isysroot /Applications/ 
-F/Library/Frameworks  -std=gnu++17 -MD 
-MT external/ocornut/imgui/CMakeFiles/imgui.dir/backends/imgui_impl_sdl.cpp.o 
-MF external/ocornut/imgui/CMakeFiles/imgui.dir/backends/imgui_impl_sdl.cpp.o.d 
-o external/ocornut/imgui/CMakeFiles/imgui.dir/backends/imgui_impl_sdl.cpp.o 
-c /Volumes/Development/github/org.pongasoft/re-edit/external/ocornut/imgui/backends/imgui_impl_sdl.cpp

/Volumes/Development/github/org.pongasoft/re-edit/external/ocornut/imgui/backends/imgui_impl_sdl.cpp:71:10: fatal error: 'SDL.h' file not found

There is nothing about SDL2 in the generated compilation command (new line added to the output for clarity)

From what you show, it is the expected behavior: option -F/Library/Frameworks is specified. So headers should be under /Library/Frameworks/SDL2.framework/Headers and you must specify headers in the form '<SDL2/SDL.h>` (this is the conventions for framework consumption):

#include <SDL2/SDL.h>

I had to change the code I am including (not mine) from imgui to make it work but after this change I confirm that it works. Thank you!