0

I am trying to install SDL2 for my C programs and I can't seem to get the library working. So far, I've installed "SDL2-devel-2.0.14-mingw.tar.gz" and followed this website's instructions:
https://w3.cs.jmu.edu/bernstdh/web/common/help/cpp_mingw-sdl-setup.php

When I try to run and compile this test c file:

#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 this error on VSCode:

C:\Users\kid\AppData\Local\Temp\cc6KBeOh.o:test.c:(.text+0xe): undefined reference to `SDL_Init'
C:\Users\kid\AppData\Local\Temp\cc6KBeOh.o:test.c:(.text+0x13): undefined reference to `SDL_Quit'
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status

I am confused as I copied and pasted the needed SDL files into MinGW and I also have SDL2.dll in my file directory.

  • If you're sure your build command is exactly `g++ -o test.exe test.c -lmingw32 -lSDL2main -lSDL2` and that's the only output you get (better check manually in cmd), then you probably copied wrong libraries (e.g. 32bit libs for 64bit compiler). – keltar Feb 09 '21 at 05:02
  • Thanks! That solved my issue in cmd. Now I just need to change vscode so it compiles with those arguments. – kidfriendly Feb 09 '21 at 06:06

1 Answers1

0

I am very late to answering this, but to future people who may have this problem here is my solution:

Add #define SDL_MAIN_HANDLED to your code. The new code will look like this:

#include "SDL2/SDL.h"
#define SDL_MAIN_HANDLED

int main( int argc, char* args[] ) {
  SDL_Init( SDL_INIT_EVERYTHING ); //Start SDL
  SDL_Quit(); //Quit SDL
  return 0;
}
  • 1
    This is not how you're supposed to do it. On Windows you can get away with it (SDL's custom `main` doesn't do much here), but there are platforms where it won't work. Better learn the intended way (which should work out of the box, if you do everything right). This might help: https://stackoverflow.com/a/64396980/2752075 – HolyBlackCat Jun 05 '23 at 06:55