How do you compile a program that uses SDL

So I’ve downloaded the sdl development library for macOS from this website and I’ve put it in the Library/Frameworks folder.

Now I’ve made a very simple program which should open an empty window and close it after 3 seconds.

#include <SDL.h>
#include
using namespace std;

int main(int argc, char * argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_CreateWindow(“My window”, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640,480,SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
SDL_Delay(3000);
SDL_Quit();
return 0;
}

I compile with the following statement:

g++ -I /Users/lschlick/Library/Frameworks/SDL2.framework/Versions/A/Headers test.cpp

I get the following errors:

Undefined symbols for architecture x86_64:
“_SDL_CreateWindow”, referenced from:
_main in test-a559f2.o
“_SDL_Delay”, referenced from:
_main in test-a559f2.o
“_SDL_Init”, referenced from:
_main in test-a559f2.o
“_SDL_Quit”, referenced from:
_main in test-a559f2.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

It seems like the compiler can’t find any definition for the functions that I’ve used in my program. How do I fix this?

Hi,

In addition to giving g++ the path of the headers, you need to give it the path to the library itself with “-L/the/path/to/lib” and finally you need to link the actual library with “-lSDL2”. (Lowercase L)

The reason is, currently g++ has no idea what the SDL functions are because despite being defined in the headers you don’t add the “content” of the SDL functions without linking libSDL2.a.

Now since you’ve mentioned being on macOS. If you have Homebrew install, I’d recommend installing SDL through it so you don’t have to go through the whole “-I/path/to/headers/ -L/path/to/lib”. All you’d have to do is “-lSDL2”.

Hope this helps!

Sorry, I can’t seem to figure out which of these files is the library itself. These are all the files (minus the headers) which came with the download.

Also I tried using homebrew to install SDL and I just used “g++ -lSDL test.cpp” to compile like you said and now it says that it can’t find the file “SDL.h”

Try <SDL2/SDL.h> instead.

I see the problem with your original issue. The devel package from libsdl.org provides a framework that you can link using Apple Clang/GCC using:
g++ -framework SDL2 test.cpp

NB: I haven’t tried myself, but it might be SDL2.framework instead of just SDL2

Nothing is working, I’ve tried every combination of these actions and the compiler can’t find any of the files no matter what I do.

Also what’s confusing is that homebrew says that sdl2 is installed at /usr/local/Cellar/sdl2/2.0.14_1, yet I can’t find the actual location /usr/local/Cellar/sdl2/2.0.14_1 anywhere on my computer. It doesn’t exist.

My bad, it’s been a slow Sunday.

I’ve grabbed my macbook and looked into it. Homebrew should symlink the libraries in /usr/local/lib so a ls /usr/local/lib | grep "SDL" should give you some results. And I got the lib name wrong, it’s actually libSDL2.a so the g++ command is:
g++ -lSDL2 test.cpp

Hopefully it works :sweat_smile:

Okay thank you my program compiles and runs now.

1 Like