Including SDL2 and SDL_mixer in a project

Hi, I decided to realize an academic project using SDL. My teacher wants to build my project without installing SDL2 and SDL_mixer in his system (Ubuntu). Therefore I need to properly include the 2 libraries into my project, buy I don’t know how to do that. Anyone can help me?

Hi and welcome to the forum!

First of all, you need to tell us which IDE/compiler you’re using. Each IDE/compiler have their own way of adding a dependency into a project, different menus.

Second, it’s a bit unclear what you mean regarding that you’re not allowed to install the library on the computer.
You need to have the SDL library downloaded to the computer to be able to add it into a project.
Are you allowed to have the library downloaded on the computer?

1 Like

I’m not using an IDE. I and my teacher will compile the project with gcc.
My teacher wants that I put the header files and the Dynamic libraries of SDL2 and SDL_mixer inside my project (a folder that contains the source codes of my application and all the images and sound that it uses). In this way he can compile my project without installing SDL2 and SDL_mixer himself.

Alright, good. So are you supposed to put the SDL- and SDL_Mixer source code inside the source folder, and then later compile it together with the project, or are you supposed to put the precompiled *.lib files together with the project?
Either way works but option number 2 is recommended.

Do you get any error message(s) during compiling?

1 Like

The second option.
I didn’t get any error while compiling, but when my teacher tried to compile his linker complains of undefined reference. Specifically:

./lib/libSDL2.so: undefined reference to exp@GLIBC_2.29' ./lib/libSDL2.so: undefined reference to log@GLIBC_2.29’
./lib/libSDL2.so: undefined reference to `pow@GLIBC_2.29’
collect2: error: ld returned 1 exit status

I think this is the problem: SDL2 depends on other libraries that my teacher hasn’t or he has in different versions.

Whats the command he is using to build ?

1 Like

I put the header files of SDL and SDL_Mixer in a folder called ‘include’ and the lib files in a folder called ‘lib’.
I created a Makefile that contains the following instructions:

LINKER_FLAGS = -L./lib -lSDL2main -lSDL2 -lSDL2_mixer
INCLUDE_PATH = -I./include

bin/test : src/test.o
gcc src/test.o $(LINKER_FLAGS) -o bin/test

src/test.o : src/test.c
gcc -c src/test.c $(INCLUDE_PATH) -o src/test.o

I installed SDL and SDL_Mixer from the source codes with these comands:

./configure --prefix=“path of the folder of my project”
make
make install

What command is your teacher running to try build your app and also what the operating system ?

1 Like

Ubuntu, I don’t know the version. He used ‘make’ with my Makefile to build my project.

I think I understand now,
Can you let us know what the structure of your Projects folders are like and are the libs and headers contained inside ?

1 Like

The folders inside the project are:

  • src : contains the source files and header files that I wrote
  • lib : contains the SDL and SDL_Mixer lib files
  • include: contains the header files of SDL and SDL_Mixer
  • bin: will contain the executable
  • img, music: two folders that contain the images and soundtracks of the project

In the main folder I put the Makefile I posted few messages ago.

I think because your teacher is not willing to install SDL2 he will be missing some flags that need to be set.

adding -lm lib should fix the current issues but you may come across more.

-lm -L./lib -lSDL2main -lSDL2 ........

Use can also use the -v flag which should show all libs used in compiling,
pthread will be another that may need to be added.

1 Like

Then, when linking I have to add the flags of all the libraries that SDL and SDL_Mixer depends on?

Possibly, that is my guess.

Looking in the SDL_Config file I can see.
I am running mingw windows.

-L${exec_prefix}/lib -lmingw32 -ldxerr8 -ldinput8 -lshell32 -lsetupapi -ladvapi32 -luuid -lversion -loleaut32 -lole32 -limm32 -lwinmm -lgdi32 -luser32 -lm -Wl,--no-undefined -pipe -lmingw32 -lSDL2main -lSDL2 -mwindows

1 Like

Is there a way, when I install these libraries into my project, to statically insert into the SDL and SDL_Mixer lib files all their dependencies?
In a way that my project just depends on SDL and SDL_Mixer and it doesn’t depends on that other libraries (lm, pthread, etcs).

SDL2 does depends on pthread for linux etc, not sure which other libs.
But the libs it depends on should already be installed on the platform / os.

1 Like

Then my teacher must have them. But if my teacher has these libraries in different versions than mine, is there any problem?

Probably not, I think you just need to find out whats not automagically been linked by SDL2 on your machine and add them.

Starting with -lm and work your way through till it builds.

1 Like

My teacher said me he won’t try again to build my project. The next time will be the official one :’(

I didn’t understand why he cannot just install SDL and SDL_Mixer in his system. I think I will resign myself and change the project with one that uses only the C standard library.

THANK YOU FOR YOUR ANSWERS :slight_smile: