SDL2 on Xcode 12

Hello,

I have been trying to follow the official documentation as well as a wide-range of material on the web to simply compile a “Hello World” program using Xcode 12.

Unfortunately, I run into the issue where the compiler complains it cannot find sdl.h when importing <SDL2/sdl.h>.

I know it’s possible to pull in SDL using home-brew and then compile on the command-line. However, I want to use Xcode so that I can easily bundle SDL as part of the application. Also, since Xcode isn’t beta software – it would be useful to others to update this information in general.

Has anyone had luck with this? I’m not typically a Mac-specific developer so Xcode is a little alien to me. I’m happy to work together if anyone has ideas or suggestions.

Thanks!

It’s years since I had a go at SDL with Xcode (I gave up cos I couldn’t figure out how to upload the app to the MacStore and thought the sales there wouldn’t be great anyway) but all I can think is did you add all the SDL Frameworks to your project?

Thanks Sean! Yeah – adding the SDL Framework is exactly what I did as far as I know.

When I get some free time, I’ll try to put together a short video or some useful screenshots to see if anyone might have suggestions. Honestly I’m a complete novice when it comes to Xcode.

In Finder (not the command line), copy SDL2.framework to the same folder as MyProject.xcodeproj

In Xcode, in your project’s settings, under the General tab, go down to the Frameworks, Libraries, and Embedded Content section. Hit the + to add another framework, hit the Add Other... -> Add Files... button, and select your SDL2.framework

In the Build Settings tab, find the Framework Search Paths build setting, double-click the text field next to it, and add the path “$(PROJECT_DIR)” (yes, actually type $(PROJECT_DIR) ) under $(inherited)

Everything should work fine now.

I have the same issue but the directions by sjr does not work for me. Xcode still says “SDL2/SDL.h” file not found.
Any other suggestions?

It needs to be `#include <SDL2/SDL.h>" with angle brackets

So I became really frustrated with this problem and went through everything several times, and I think I can get it to work. First of all, if you’re really certain you’ve done everything correctly, try cleaning the build folder from the Product menu. If not, clean it after these steps. I’m using the normal .framework installation from the official SDL site (not brew).

  • In the Target settings (under Project settings), click on the Build Settings tab, and click “All.”
  • search for “search”
  • Point Framework Search Paths to /Library/Frameworks (this is the same as $(LOCAL_LIBRARY_DIR)/Frameworks), or where you’ve installed it.
  • Point Headers to /Library/Frameworks/SDL2.framework/Headers (this might be unnecessary)
  • Go to “Build Phases” tab, and under “Link Binary With Libraries”, add SDL2.framework from Add Other… > Add Files (this should be unnecessary for a hello world project)
  • #include <SDL2/SDL.h> (try typing out to see if autocomplete works)
  • Clean again and see if it builds

(Also, I’m using xcode 11 on mojave, so it’s a bit of an old version, apologies)

You don’t need to add the framework headers to the header search path, since that’s the whole point of macOS frameworks, the compiler and operating system know where they are inside the framework so long as they can find the framework.

Also, probably don’t install the framework to a system path. Put it alongside your .xcodeproj, that way it’ll be versioned with it (I don’t remember what Xcode 11 does, but Xcode 12 automatically includes it in your app bundle if you link to it, so users won’t have to have it installed).

1 Like