C++ SDL2 on Raspberry Pi. Cannot link to library.

Trying to get SDL2 to work with C++ on a Raspberry Pi 3B+ but don’t know how to get it to link to the SDL2 libraries.

Not hugely familiar with Linux.

I installed SDL2 as follows:

sudo apt-get install libsdl2-2.0-0. Says it is already installed.

sudo apt-get install libsdl2-dev.

Then I created the folder /home/pi/C++/SDL2Test1.

Using geany I wrote the following code and saved it to SDL2Test1.cpp:

#include <stdio.h>
#include "SDL2/SDL.h"

int main( int argc, char* args[] )
{
	SDL_Window* window = NULL;
	SDL_Surface* screenSurface = NULL;
	if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
		printf( "SDL could not be initialized! SDL_Error= %s\n", SDL_GetError() );
	}
	else {
		puts( "SDL initialized OK!" );
	}
    return 0;
}

I set up the build command to do this:

g++ -Wall -std=c++0x -lSDL2 SDL2Test1.cpp -oSDL2Test1

Then I did a build, and got this:

Ignore the warnings. It failed to link to the SDL2 library.

What do I need to do to fix this? I’m not entirely certain which folder contains the SDL2 library. If I knew then I was thinking that I could do this:

cd ~
nano .profile

then go to the end of the file and add something like this:

PATH="/lib/SDL2:$PATH"

but I cannot figure out the folder I need to specify.

It would be nice to modify the INCLUDE environment variable as well, so I could just use:

#include "SDL.h"

instead of

#include "SDL2/SDL.h"

but that’s a minor annoyance.

I feel like there is something else I needed to do after installing SDL2 to make it work more smoothly with g++, but I don’t see anything in the tutorials I have followed.

So:

  1. Is this the correct approach?
  2. Where is the SDL library folder so I can include it in the PATH?
  3. How do I specify where to find include files?

Any help appreciated.

I think this is an issue with the order that the libs are provided in your compile command. It is kind of a common issue with a setting that Debian/Raspbian turn on called “Smart Linker” in the Gnu Linker. Try this order instead:
g++ -Wall -std=c++0x SDL2Test1.cpp -lSDL2 -oSDL2Test1

Thanks for answering. That works! I still have a problem finding the include files which I thought I could solve by using sdl2-config, but that has solved the major problem.

This also works:

g++ -Wall -std=c++0x SDL2Test1.cpp $(sdl2-config --cflags) -lSDL2 -oSDL2Test1

However, if I change the command to this:

g++ -Wall -std=c++0x SDL2Test1.cpp $(sdl2-config --cflags --libs) -oSDL2Test1

I get the messages:

/usr/bin/ld: skipping incompatible /usr/local/lib/libSDL2.so when searching for -lSDL2
/usr/bin/ld: skipping incompatible /usr/local/lib/libSDL2.a when searching for -lSDL2
/usr/bin/ld: cannot find -lbcm_host: No such file or directory
collect2: error: ld returned 1 exit status
Compilation failed

So for some reason using sdl2-config does not work with --libs. Have you any idea why?

If I do this at the command line:

sdl2config --cflags --libs

I get this:

-I/usr/local/include/SDL2 -I/opt/vc/include -I/opt/vc/include/interface/vcos/pthreads -I/opt/vc/include/interface/vmcs_host/linux -D_REENTRANT
-L/usr/local/lib -Wl,-rpath,/usr/local/lib -lSDL2 -L/opt/vc/lib -lbcm_host -lpthread

From what I have seen the culprit is bcm_host. A few Google searches suggested that I could install bcm_host by doing this:

sudo apt install libraspberrypi-dev raspberrypo-kernel-headers

This doesn’t work. It says they have been replaced by libdtov10:armhf and libdtov10. I cannot install this either. Cannot find it.

So I Googled “cannot install libraspberrypi-dev”. The AI overview suggests that libraspberrypi-dev is “obsoleted” and I need to go back to an earlier version of Raspberry Pi OS - Debian Bullseye. I was about to try that when I saw your reply.

This looks like more of a Pi problem than an SDL2 one, so I may just stick with your solution.

Thanks again.

I think that you’ve nailed it.
From what I see, the bcm_host library was used to access the gpu and hardware more directly.
I’m pretty sure that in its absence SDL2 is still able to use X11/Wayland calls for their GPU accelerated rendering API (renderer and textures), so I don’t think it will affect the program too much if it is simply removed from the compile commands.

You might try adding the other settings/args from the sdl2config output into a Makefile to try to maintain the rest of their recommended build settings.

So there’s a simple work-around, but I’m pretty sure this is something that the devs would like to fix in source;
How do you feel about issuing a bug report on SDL’s Github?