Using homebrew installed SDL2 with Xcode
Asked Answered
W

4

19

I have installed SDL2 using Homebrew but now I don't know how to make sure Xcode can use it! I imported the created library and added it to the build phases tab of my project. But when I try to build I get the error 'SDL2/SDL.h' not found

Widen answered 19/1, 2015 at 0:1 Comment(0)
U
35

To be able to use SDL2 on Xcode you must set two things (which are required for SDL in general):

  • where to find header files (so that Clang can compile with -Iheader/path)
  • where to find the .dylib to link it to the project (since with brew you don't have a real .framework)

To know the correct paths you should invoke sdl2-config --cflags and sdl2-config --libs. On my system these produce:

:~jack$ /usr/local/bin/sdl2-config --cflags
-I/usr/local/include/SDL2 -I/usr/X11R6/include -D_THREAD_SAFE

:~jack$ /usr/local/bin/sdl2-config --libs
-L/usr/local/lib -lSDL2

Now just paste the first one into other C flags and the other one into other linker flags field of your project and you are ready to go.

You could set them up in the correct fields, which is Header Search Paths for -I and Library Search Path for -l but the result will be the same.

Uitlander answered 19/1, 2015 at 0:15 Comment(4)
Sorry been really busy so I couldn't respond! Thanks for the answer a few questions though when I add the paths to other C flags and other linker flags I can access SDL but is it not nested like #include <SDL2/SDL.h> How can I make sure this works? And also what paths should I put into header search paths and library search paths if I wanted to do it the correct way? Because I tried doing it but didn't get it to work just yet.Widen
Oh and also how do I get this to work with SDL_ttf as well? Because I can't find it! The nesting of SDL2/SDL.h and using the correct field worked out though thanks!Widen
Ok nevermind I just made a few mistakes sorry! Thanks for the answer!Widen
Just a side note, if you add only the header and lib directories to your project's Headr Search Paths and Library Search Paths, you will get error, respectively, because in order to Xcode the use those libraries, you need to tell Xcode that, for example, use -lSDL2 library, so I advise you to use the first method that the above answer provides.Birthright
O
4
  1. Install installed homebrew from https://brew.sh

  2. Type in terminal brew install sdl2

  3. Then show the path of framework (in xCode select project file >> build settings >> header search paths) and using cmd+shift+g type /usr/local/include

  4. In 'General' 'Frameworks & Libraries' put libSDL2-2.0.0.dylib (its here /usr/local/Cellar/sdl2/2.0.14_1/lib)

  5. And most important check 'Disable Library Validations' in 'Signing & Capabilities'

After these steps code started to work for me.

Oleum answered 13/1, 2021 at 1:49 Comment(1)
Don't duplicate your answers. If you (or someone else) decide to update/modify it later, you'll have to go over ever single one. I suggest posting it once, then posting a link in comments of the other question.Wilinski
H
1
  1. brew search sdl2

  2. brew install sdl2 sdl2_image sdl2_mixer sdl2_net sdl2_ttf

  3. config xcode Build Settings --> All --> Search Paths --> Header Serch Paths
    --> /usr/local/include

  4. config Xcode General ->add Frameworks and Libraries --> libSDL2-2.0.0.dylib

  5. test your code

#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

using namespace std;

int main() {

    if(SDL_Init(SDL_INIT_VIDEO) < 0) {
        cout << "SDL init failed." << endl;
        return 1;
    }

    cout << "SDL Init succeeded." << endl;

    SDL_Quit();

    return 0;
}
Homeopathic answered 1/5, 2020 at 19:28 Comment(0)
E
1

Including SDL2 as <SDL2/SDL.h> is incorrect. It should be included as <SDL.h>. Now sdl2-config --cflags will give you the correct path.

The former only works if SDL2 has been installed into one of the default include paths, which is not the case with homebrew (but common in Linux which is why you see the former so much in the wild).

(One can argue it would have been better if SDL2 had put its headers into a sub-directory, but they didn't).

Ernestoernestus answered 14/5, 2024 at 6:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.