# Including SDL2 as a Framework (macOS) help

**URL:** https://discourse.cmake.org/t/including-sdl2-as-a-framework-macos-help/5481
**Category:** Code
**Created:** [April 17, 2022, 6:03pm UTC](https://discourse.cmake.org/t/including-sdl2-as-a-framework-macos-help/5481 "2022-04-17T18:03:23Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![fry](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fry/32/613_2.png) [@fry](https://discourse.cmake.org/u/fry)
#### Post date: [April 17, 2022, 6:03pm UTC](https://discourse.cmake.org/t/including-sdl2-as-a-framework-macos-help/5481/1 "2022-04-17T18:03:23Z")

</div>

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:

```auto
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

```auto
-- 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:

```auto
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.

Thanks

---

<div class="post-metadata">

### Author: ![fry](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fry/32/613_2.png) [@fry](https://discourse.cmake.org/u/fry)
#### Post date: [April 17, 2022, 6:12pm UTC](https://discourse.cmake.org/t/including-sdl2-as-a-framework-macos-help/5481/2 "2022-04-17T18:12:07Z")

</div>

Note that if I change to this:

```auto
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:

```auto
: && /Applications/Xcode11.3.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -g -isysroot /Applications/Xcode11.3.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -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/main.mm.o -o re-edit external/ocornut/imgui/libimgui.a -framework SDL2 && :
ld: framework not found SDL2

```

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [April 17, 2022, 8:44pm UTC](https://discourse.cmake.org/t/including-sdl2-as-a-framework-macos-help/5481/3 "2022-04-17T20:44:46Z")

</div>

Use instead:

```cmake
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).

---

<div class="post-metadata">

### Author: ![fry](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fry/32/613_2.png) [@fry](https://discourse.cmake.org/u/fry)
#### Post date: [April 18, 2022, 2:36pm UTC](https://discourse.cmake.org/t/including-sdl2-as-a-framework-macos-help/5481/4 "2022-04-18T14:36:11Z")

</div>

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

```auto
FAILED: external/ocornut/imgui/CMakeFiles/imgui.dir/backends/imgui_impl_sdl.cpp.o
/Applications/Xcode11.3.1.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  
-I/Volumes/Development/github/org.pongasoft/re-edit/external/ocornut/imgui -g 
-isysroot /Applications/Xcode11.3.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk 
-mmacosx-version-min=10.14 
-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)

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [April 18, 2022, 2:53pm UTC](https://discourse.cmake.org/t/including-sdl2-as-a-framework-macos-help/5481/5 "2022-04-18T14:53:31Z")

</div>

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):

```nohighlight
#include <SDL2/SDL.h>

```

---

<div class="post-metadata">

### Author: ![fry](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fry/32/613_2.png) [@fry](https://discourse.cmake.org/u/fry)
#### Post date: [April 18, 2022, 3:05pm UTC](https://discourse.cmake.org/t/including-sdl2-as-a-framework-macos-help/5481/6 "2022-04-18T15:05:50Z")

</div>

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!
