Set up SDL2 on your Mac without Xcode

This is a super simple guide to get SDL2 installed and linked into your project so you can get to the fun stuff (game development) quickly. Created with massive amounts of help from Deacon Blues.

1. Install SDL2

In your terminal, run the brew command:

brew install sdl2

This will fetch a compiled version of SDL2, so you don’t need to worry about any of the compilation steps mentioned in the SDL docs (https://wiki.libsdl.org/Installation).

2. Create a project

mkdir myproject
cd myproject
touch main.cpp
touch Makefile
mkdir include
mkdir lib

In myproject/main.cpp:

#include <iostream>
#include <SDL2/SDL.h>
using namespace std;
int main() {
cout << "Hello World!";
SDL_Init(SDL_INIT_VIDEO);
return 0;
}

3. Move the SDL files into your project

It might help to use Finder for this part. Navigate to the place where SDL2 installed. For me, that was:

/usr/local/Cellar/sdl2/2.0.5

Copy the contents of

/usr/local/Cellar/sdl2/2.0.5/include/

(It should be a folder called SDL2 that contains a bunch of .h files.)

and paste it into

myproject/include

Then, go to

/usr/local/Cellar/sdl2/2.0.5/lib/

and copy the contents (some folders and some .a and .dylib files) into

/myproject/lib

4. Create your Makefile to compile using SDL2

In myproject/Makefile:

game:
g++ main.cpp -o play -I include -L lib -l SDL2-2.0.0

Note: Flags are used to link SDL2 to your project.

`-I` (i as in include) tells it additional include directories you want to add

`-L` tells it additional library directories you want to add

`-l` (lowercase l as in lib) tells it specific library binaries you want to add

5. Make your project

In your project’s directory, in the terminal, run:

make game

This will compile the game.

6. Run your project

./play

This step runs the executable created by compiling. If all went well, you should see a nice “Hello World!” print. That means you’ve got SDL2 hooked up. Congrats!!

All set! Now what?

This tutorial is pretty awesome for actually using SDL2:

And, if you’re wondering what SDL2 even is…