38

Here's a piece of my current Makefile:

CFLAGS = -O2 -Wall -pedantic -std=gnu++11 `sdl-config --cflags --libs` -lSDL_mixer 

I have libsdl installed properly, SDL.h is in /usr/include/sdl where it belongs, but it just won't compile. I also have the line #include "SDL.h" in my .h files, but still no go.

Anyone knows why?

1
  • Please include the following in your question: (a) the actual literal output of sdl-config --cflags --libs and (b) the actual literal full path to SDL.h. Commented Oct 12 at 2:24

7 Answers 7

75

For Simple Direct Media Layer 2 (SDL2), after installing it on Ubuntu 16.04 via:

sudo apt-get install libsdl2-dev 

I used the header:

#include <SDL2/SDL.h> 

and the compiler linker command:

-lSDL2main -lSDL2 

Additionally, you may also want to install:

apt-get install libsdl2-image-dev apt-get install libsdl2-mixer-dev apt-get install libsdl2-ttf-dev 

With these headers:

#include <SDL2/SDL_image.h> #include <SDL2/SDL_ttf.h> #include <SDL2/SDL_mixer.h> 

and the compiler linker commands:

-lSDL2_image -lSDL2_ttf -lSDL2_mixer 
Sign up to request clarification or add additional context in comments.

2 Comments

On linux, simply change #include <SDL.h> to #include <SDL2/SDL.h> fixed it, of course on compiling need -lSDL2 flag.
In mac , get it using homebrew : brew install sdl2
30

If the header file is /usr/include/sdl/SDL.h and your code has:

#include "SDL.h" 

You need to either fix your code:

#include "sdl/SDL.h" 

Or tell the preprocessor where to find include files:

CFLAGS = ... -I/usr/include/sdl ... 

1 Comment

The way to get the correct preprocessor flags is to use sdl-config --cflags --libs in the command line, which OP did.
12

The header file lives at /usr/include/SDL/SDL.h (SDL1) or /usr/include/SDL2/SDL.h (SDL2).

In your C++ code, pull in this header using

#include <SDL.h> // for SDL1 
#include <SDL2/SDL.h> // for SDL2 

You have the correct usage of

sdl-config --cflags --libs # sdl1 
sdl2-config --cflags --libs # sdl2 

which will give you

# SDL1 -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -L/usr/lib/x86_64-linux-gnu -lSDL 

or

# SDL2 -I/usr/include/SDL2 -D_REENTRANT -lSDL2 

At times you may also see this usage which works for a standard install:

pkg-config --cflags --libs sdl # sdl1 
pkg-config --cflags --libs sdl2 # sdl2 

which supplies you with

-D_GNU_SOURCE=1 -D_REENTRANT -I/usr/include/SDL -lSDL # SDL1 

or

-D_REENTRANT -I/usr/include/SDL2 -lSDL2 # SDL2 

Comments

8

Most times SDL is in /usr/include/SDL. If so then your #include <SDL.h> directive is wrong, it should be #include <SDL/SDL.h>.

An alternative for that is adding the /usr/include/SDL directory to your include directories. To do that you should add -I/usr/include/SDL to the compiler flags...

If you are using an IDE this should be quite easy too...

Comments

3

the simplest idea is to add pkg-config --cflags --libs sdl2 while compiling the code.

g++ file.cpp `pkg-config --cflags --libs sdl2`

Comments

-1

Having a similar case and I couldn't use StackAttacks solution as he's referring to SDL2 which is for the legacy code I'm using too new.

Fortunately our friends from askUbuntu had something similar:

Download SDL

tar xvf SDL-1.2.tar.gz cd SDL-1.2 ./configure make sudo make install 

Comments

-1

I had this problem today, and SDL2 did not work for me.

If you need SDL1 you can install it with:

sudo apt install libsdl1.2-dev 

Tetsted on xubuntu 24.04.1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.