Search code examples
cgccsdl-2

how do i resolve this error when compiling C code that includes SDL


when i try to compile the following C code with gcc -I/SDL2-2.0.20/x86_64-w64-mingw32/include hsdl.c -L/SDL2-2.0.20/x86_64-w64-mingw32/lib in windows

#include "SDL2/SDL.h"
int main( int argc, char* args[] ) {
  SDL_Init( SDL_INIT_EVERYTHING ); //Start SDL
  SDL_Quit(); //Quit SDL
  return 0;
}

i get the following errors

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PC\AppData\Local\Temp\ccfFajdG.o:hsdl.c:(.text+0x15): undefined reference to `SDL_Init'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PC\AppData\Local\Temp\ccfFajdG.o:hsdl.c:(.text+0x1a): undefined reference to `SDL_Quit'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function `main':
C:/_/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

i did install SDL headers and library files manually by moving the files to the library and include paths.


Solution

  • You need to tell gcc where to find those include files and where to find required libraries for SDL2.

    Compile your program like this:

    gcc `pkg-config --libs --cflags sdl2` ./hsdl.c -o ./hsdl.exe
    

    If you haven't installed SDL2 on msys2 use this command:

    pacman -S mingw-w64-x86_64-SDL2
    

    If you haven't installed pkg-config on msys2 use this command:

    pacman -S mingw-w64-x86_64-pkg-config