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
2

1 Answer 1

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.

1
  • It was almost right! The flags I needed to use was these: -lmingw32 -LC:\MinGW\include\SDL2\lib -lSDL2main -lSDL2 in that exact order! Sep 23, 2013 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.