1

I keep getting the error "undefined reference to WInMain@16" when I include SDL2/SDL.h in my C file. It's a simple "Hello" program with the SDL include, and if I remove the SDL include it compiles just fine (as expected).

The problem is I'm new with the compile flags for C (and SDL) and I'm not sure how I link(?) the files together (or if that's necessary). I'm coding using Sublime Text 3 so I'm not sure how you would link SDL as you would when using an IDE.

(D:\CODE\Privata Projekt\C\test.c)
#include <stdio.h>
#include "SDL2/SDL.h"

int main(int argc, char *argv[]) {
    printf("hello\n");
    return 0;
}

My paths to MinGW and SDL2 is:

C:\MinGW\include\SDL2 (all my sdl header files reside in here too)
C:\MinGW\include\SDL2\bin
C:\MinGW\include\SDL2\lib
C:\MinGW\include\SDL2\share

And I build the program with

gcc test.c -o test

EDIT: What worked for me was to use these flags, in this exact same order

   -lmingw32 -LC:\MinGW\include\SDL2\lib -lSDL2main -lSDL2
user1021726
  • 638
  • 10
  • 23
  • possible duplicate of [SDL 2 Undefined Reference to "WinMain@16" and several SDL functions](http://stackoverflow.com/questions/17048072/sdl-2-undefined-reference-to-winmain16-and-several-sdl-functions) – Some programmer dude Sep 19 '13 at 13:52
  • That thread simply states "you are probably not linking". As I wrote I'm not sure how I would link, if that is the problem, and I'm not using an IDE – user1021726 Sep 19 '13 at 15:04

1 Answers1

1

You need to link with the library as well. You can do it by passing the correct options on the command line: -L to tell the linker where to find the library, and -l (lowercase L) to tell the linker to link to the library.

Like

> gcc test.c -o test -LC:\MinGW\include\SDL2\lib -lSDL2

(I don't know the name of the library, so change SDL2 to the appropriate name.)


If there is problem running your program due to the loader not finding the SDL2 library, you may have to add another option which tells linker the place of the dynamic library:

> gcc test.c -o test -LC:\MinGW\include\SDL2\lib -Wl,-rpath=C:\MinGW\include\SDL2\lib -lSDL2

I don't know if it's needed or even used on Windows though. You might have to copy the DLL to the directory where the executable is.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • It was almost right! The flags I needed to use was these: `-lmingw32 -LC:\MinGW\include\SDL2\lib -lSDL2main -lSDL2` in that exact order! – user1021726 Sep 23 '13 at 08:22