1

I have recently downloaded SDL2 and am trying to install it. I created a small test program consisting of a .cpp and a .h file.

I have put SDL2.frameworks inside of /Library/Frameworks and then made sure that it was added to General->Frameworks and Libraries and tried to #include <SDL2/SDL.h>. file not found.

I made sure to go to the build settings->Search Paths->Framework Search Paths to add /Library/Frameworks (it was already listed next to the setting but I readded it to the dropdown). I also added /Library/Frameworks/SDL2.framework to the Header Search Paths. file not found.

I tried to include <SDL2/SDL.h>, <SDL/SDL.h>, <SDL.h>, "SDL2/SDL.h", "SDL/SDL.h" and "SDL.h". none of these files were found.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • can you verify the steps taken against the ones descibed at Lazy Foo Productions: https://lazyfoo.net/tutorials/SDL/01_hello_SDL/index.php ? That's the most comprehensive SDL tutorial site I know of – lucasreta Feb 22 '21 at 05:46

2 Answers2

1

For the header search path, it should be set to FRAMEWORK_DIRCTORY/SDL2.framework/Headers/, or you can use FRAMEWORK_DIRCTORY/SDL2.framework and turn on recursive search.

Then you should be able to use it with just #include <SDL.h>.


Edit:

After some investigation, seems like my original answer was more of a workaround. The proper way of including SDL library in your code should be using just #include <SDL2/SDL.h>, however it wasn't working.

The reason of that is when compiling the code, Xcode would attempt to copy the library to the product folder, so the compiled executable can have easy access to it. However, for some reason, Xcode copied the framework without the header folder (the reason is probably that Xcode is moving towards using Swift only, which doesn't really have a "header" thing).

By default, when you try to run the code, and it sees there is a SDL2.framework folder located in the product folder, it would use that framework, even if it doesn't have the header folder located in it. But since it doesn't actually have the header folder in it, it doesn't actually run.


To solve it, the easiest way is to remove it from the Targets/Build Phases/Embed Frameworks.

By default when you add a framework to your project, it would also be added here. By removing it from the Embed Frameworks, it won't copy the framework to product folder. And it will only attempt to search SDL framework from whatever you have put in the Build Settings/Framework Search Path.

In the same time, you won't need to have anything for Build Settings/Header Search Path, as it will look for Headers folders in your frameworks first.

And with you code, you can just use #include <SDL2/SDL.h>.


Even better, you can add the framework to Build Phases/Copy Files, and set the Destination to Frameworks, empty the Subpath, and potentially uncheck Copy only when installing, for better cross device usabilities.

Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39
  • @slitbodmod I've just made some long edited to the answer. My original one was more of a workaround. The new one solves it more properly. Let me know if that works and accept the answer if you like it :D – Ranoiaetep Feb 17 '21 at 23:35
  • Ah thankyou, the previous answere worked until I installed the SDL_ttf extension. I did what you said in the edit and it seems to build ok but now it's thrown a code sign error :| – slit bodmod Feb 19 '21 at 00:32
  • @slitbodmod SDL_ttf should work similar to SDL, you can just remove it from embed frameworks, and use `#include ` in your code. It might show an error about it's not published by safe source or something, which you can allow it in `System Preference/Security/General/Allow apps/Allow anyways`. – Ranoiaetep Feb 19 '21 at 00:48
  • I just unchecked the Code Sign on copy box for all of the libraries and that seemed to work – slit bodmod Feb 19 '21 at 00:57
  • None of the above worked for me (as of 2023-04-21). Deleting the SDL2 framework from "Embed Frameworks" is enough to make the app build, but it crashes at runtime. What worked for me was to add a new "Headers" build phase, and put SDL2.framework in its "Private" section. – RudolfW Apr 22 '23 at 21:33
0

There's something really strange about the SDL2_ttf framework. While this is NOT the "right" way to do it, I found everything was fine using the default XCode settings as long as #included the absolute path the header:

#include "/Library/Frameworks/SDL2_ttf.framework/Headers/SDL_ttf.h"

In my case, the library then worked fine. This may corroborate the idea of the header not getting copied into the local Frameworks directory.

user2465201
  • 471
  • 5
  • 10