Compiling SDL2 / image / mixer / ttf for the Raspberry Pi without X11

If you want accelerated SDL2 graphics when on the console, e.g. on Raspbian Lite you don’t want to use X11. You could get away with just installing SDL2 from the repository, but my SDL2 version had problems regarding touch screens, so I needed to recompile. Also if you install any of the libsdl2* libraries a lot of unnecessary dependencies (e.g. X11) are pulled in, so it makes sense to compile SDL2 and all of SDL_image / SDL_mixer / SDL_ttf etc. yourself.
This is a walkthrough, but as always ymmv…


Uninstall all of the system-provided libraries

sudo apt remove -y libsdl2*
sudo apt autoremove -y
sudo apt autoclean

Install build tools

sudo apt install --no-install-recommends build-essential autoconf automake libtool

Build and install SDL2

Install missing dependencies for SDL2 itself:

sudo apt install libasound2-dev libudev-dev libdbus-1-dev

If you want touch screen support through tslib, make sure you install:

sudo apt install libts-dev

Download and unpack SDL 2.0.12:

wget https://www.libsdl.org/release/SDL2-2.0.12.tar.gz
tar zxf SDL2-2.0.12.tar.gz
rm SDL2-2.0.12.tar.gz

and configure it (some suggest the configure option “–disable-mir”, but it seems to be removed):

cd SDL2-2.0.12
./autogen.sh
./configure --disable-pulseaudio --disable-esd --disable-video-wayland --disable-video-opengl --disable-video-x11

This is for the RPi 1 / Zero. If you have a RPi 2 / 3 / 4 you might want to enable ARM NEON for blitters by adding “–enable-arm-neon”.
Now compile and install it. This may take some time:

make -j $(grep -c '^processor' /proc/cpuinfo 2>/dev/null)
sudo make install
cd ..

Build and install SDL_image

Install missing dependencies for SDL2_image:

sudo apt install libpng-dev libjpeg-dev

Download and unpack SDL_image 2.0.5:

wget https://www.libsdl.org/projects/SDL_image/release/SDL2_image-2.0.5.tar.gz
tar zxf SDL2_image-2.0.5.tar.gz
rm SDL2_image-2.0.5.tar.gz

and configure it (here, only with JPG and PNG support and dynamic TIFF and WEBP support):

cd SDL2_image-2.0.5
./autogen.sh
./configure --disable-bmp --disable-gif --disable-lbm --disable-pcx --disable-pnm --disable-svg --disable-tga --disable-tif --disable-xcf --disable-xpm --disable-xv --disable-webp

Now compile and install it. This may take some time:

make -j $(grep -c '^processor' /proc/cpuinfo 2>/dev/null)
sudo make install
cd ..

Build and install SDL_ttf

Install missing dependencies for SDL2_ttf:

sudo apt install libfreetype6-dev

Download and unpack SDL2_ttf 2.0.15:

wget https://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-2.0.15.tar.gz
tar zxf SDL2_ttf-2.0.15.tar.gz
rm SDL2_ttf-2.0.15.tar.gz

and configure it (here, only with JPG and PNG support and dynamic TIFF and WEBP support):

cd SDL2_ttf-2.0.15
./autogen.sh
./configure --without-x

Now compile and install it. This may take some time:

make -j $(grep -c '^processor' /proc/cpuinfo 2>/dev/null)
sudo make install
cd ..

Build and install SDL_mixer

Install missing dependencies for SDL2_mixer:

sudo apt install libflac-dev libogg-dev libmpg123-dev libmodplug-dev

Download and unpack SDL_mixer 2.0.5:

wget https://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-2.0.5.tar.gz
tar zxf SDL2_mixer-2.0.5.tar.gz
rm SDL2_mixer-2.0.5.tar.gz

Or clone it from the SDL2 GitHub mirror if you need the most recent version:

git clone https://github.com/SDL-mirror/SDL_mixer

Configure it (here, only with FLAC, MOD, MP3, OGG support):

cd SDL2_mixer-2.0.5
./autogen.sh
./configure --disable-music-midi --disable-music-opus

Now compile and install it. This may take some time:

make -j $(grep -c '^processor' /proc/cpuinfo 2>/dev/null)
sudo make install
cd ..

Uninstalling -dev packages

If you are happy with the results, you can uninstall the -dev packages for SDL2:

sudo apt remove libasound2-dev libudev-dev libdbus-1-dev libts-dev libpng-dev libjpeg-dev libfreetype6-dev libflac-dev libogg-dev libmpg123-dev libmodplug-dev

Compile programs using the new libraries

The libraries will be installed to “/usr/local/lib”. As this is not the standard path for libraries from the repositories you might get error complaining about missing .so files when you run your program. To fix that either compile normally and set run your program using:

export LD_LIBRARY_PATH=/usr/local/lib && <YOUR_PROGRAM>

or make the library path known system-wide somehow.

Another option is specifying the path when compiling your program using the “-rpath /urs/local/lib” linker option in GCC. If you use CMake, add the following to your CMakeLists.txt before add_executable():

# Add /usr/local/lib to the linker paths (-rpath for GCC)
# so self-compiled SDL2 libraries are found
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) set(CMAKE_INSTALL_RPATH "/usr/local/lib")

After that you define your executable like you usually would. Here is a complete example CMakeLists.txt:

cmake_minimum_required(VERSION 3.1)

project(myproject)
aux_source_directory(. SRC_LIST)
include_directories(.)

# C++11, add "-g" if you want to debug
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

# add /usr/local/lib to the linker paths (-rpath for GCC)
# so self-compiled SDL2 libraries are found
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_INSTALL_RPATH "/usr/local/lib")

# Add myproject executable to project
add_executable(${PROJECT_NAME} ${SRC_LIST})

# Add SDL2 includes and libraries
include(FindPkgConfig)
pkg_search_module(SDL2 REQUIRED sdl2)
pkg_search_module(SDL2IMAGE REQUIRED SDL2_image>=2.0.0)
pkg_search_module(SDL2MIXER REQUIRED SDL2_mixer>=2.0.0)
pkg_search_module(SDL2TTF REQUIRED SDL2_ttf>=2.0.0)
include_directories(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS} ${SDL2MIXER_INCLUDE_DIRS} ${SDL2TTF_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARIES} ${SDL2MIXER_LIBRARIES} ${SDL2TTF_LIBRARIES})

Links

Published by HorstBaerbel

Software developer by trade and interest, but I venture into the electronics- and diy-world from time to time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website at WordPress.com
Get started
%d bloggers like this: