Lazy Foo' Productions


Setting up SDL Extension Libraries on g++

Last Updated: Jun 21st, 2014

1)Go download the source for lesson 06. Extract the source somewhere. Now compile by entering this command:

g++ 06_extension_libraries_and_loading_other_image_formats.cpp -w -lSDL2 -lSDL2_image -o 06_extension_libraries_and_loading_other_image_formats

Now you may get an error saying it can't find SDL_image.h. For linux, we'll have to include the SDL headers like this:

#include<SDL2/SDL_image.h>

For SDL_ttf and SDL_mixer, we have to include them like this:

#include<SDL2/SDL_ttf.h>

#include<SDL2/SDL_mixer.h>

If you're using a makefile, you can just change the values of some of the macros:
From Makefile
#OBJS specifies which files to compile as part of the project
OBJS = 06_extension_libraries_and_loading_other_image_formats.cpp

#CC specifies which compiler we're using
CC = g++

#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
COMPILER_FLAGS = -w

#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lSDL2 -lSDL2_image

#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = 06_extension_libraries_and_loading_other_image_formats

#This is the target that compiles our executable
all : $(OBJS)
	$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
As you can see it was as easy as changing the file name of the source and executable and adding

-lSDL2_image

to the linker. If we were linking SDL_ttf, we'd add

-lSDL2_ttf

and for SDL_mixer we'd put:

-lSDL2_mixer

Now that you have the extension library compiling, it's time to go onto part 2 of the tutorial.