undefined reference to 'WinMain' with SDL

2019-08-08 20:09发布

问题:

I'm struggeling with my first steps in SDL. I wanted to compile a simple test class, just including the SDL2 header, nothing special for startup:

main.cpp:

#include <SDL.h>

int main() {
    return 0;
}

main.cpp itself compiles fine: g++ -c main.cpp -ISDL/include

but as soon as i want to link it with the SDL2.dll either with the machinecode main.o or directly, i'm getting this error: g++ main.cpp -o sdl_test -I SDL/include -L SDL/lib/x64 -l SDL2 -mwindows g++ -o test main.o -L SDL/lib/x64 -l SDL2 -mwindows

/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/../../../../lib/libcygwin.a(libcmain.o): In function `main':
/usr/src/debug/cygwin-1.7.30-1/winsup/cygwin/lib/libcmain.c:39: undefined reference to `WinMain'
/usr/src/debug/cygwin-1.7.30-1/winsup/cygwin/lib/libcmain.c:39:(.text.startup+0x7e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `WinMain'
collect2: Fehler: ld gab 1 als Ende-Status zurück

Additional Information: I use Cygwin and obviously g++ to compile my C++ code. My OS is Windows 7 Professional 64Bit SP 1.

I googled for several hours but all the results I came across said use -mwindows to compile a non console application or other things which didn't worked out.

回答1:

When you're using SDL, your main() must look like int main(int, char **) (or int main(int argc, char **argv)).

Why? Because, somewhere inside SDL code you can find

int SDL_main(int, char **);

int main(int argc, char **argv)
{
    /*Some stuff.*/

    SDL_main(argc, argv);

    /*Some stuff.*/
}

And then, inside SDL.h:

#define main SDL_main