Search code examples
ccompiler-errorssdl

Why linker error when trying to compile SDL?


I am watching a seminar on writing 2D games in C, on YouTube here: https://www.youtube.com/watch?v=yFLa3ln16w0&t=654s

The instructor says to install the SDL software packages, using apt-get install. I already have these files. I checked, and they are in the C search path, in the directory /usr/include/SDL2. The instructor has a GitHub with example code that demonstrates the basics of SDL. Here is the GitHub repository: https://github.com/tlively/sdl_seminar

Here is SDL's website: https://www.libsdl.org/

The problem is, when I try to compile the file hello1_sdl.c (in the GitHub repoistory) using gcc -o hello1_sdl hello1_sdl.c, it gives me an error. I looked up what it meant, and it's a linker error. I don't understand why this is happening, as the header files are in the appropriate place.enter image description here

How do I fix this?


Solution

  • The include files for the C/C++ compilers are only half the story. You also need to tell the linker to use the library! If you have everything set up properly, all you should need to do is add -lSDL2 to your final command line (the one with -o that does the linking.)

    See more examples at: Lazy Foo' Productions