1

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.

Matze
  • 533
  • 7
  • 16
  • they do some very nasty macro stuff that redefines `main`. try simply `#undef main` after the include to see if that is the case. if so then find the definition in the SDL headers, where it's explained how they intended people to disable the definition, and use that SDL way to turn of that sh*t. – Cheers and hth. - Alf Jul 05 '14 at 13:31
  • by the way, `-mwindows` merely specifies the Windows subsystem and adds some Windows API import libraries. it has nothing to do with "other things which didn't work". to get a more detailed view of this option for your compiler, do a `-dumpspecs` and filter on "mwindows". – Cheers and hth. - Alf Jul 05 '14 at 13:36
  • `#include #undef main` still doesn't work, but the header can't be the problem. As I said, the file itself compiles with the header just fine, but as soon as i try to link them i'm getting this exception – Matze Jul 05 '14 at 13:47
  • the conclusion doesn't follow from the premise. but you can just use the SDL way. remove the `#undef`, and add arguments to `main`, like this: `int main( int argc, char* argv[] )`. that plays nice with SDL macro. – Cheers and hth. - Alf Jul 05 '14 at 13:52

1 Answers1

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
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • Son all i have to do would be, as @Cheersandhth.-Alf suggested, just using `int main( int argc, char* argv[] )` as my main function? – Matze Jul 05 '14 at 14:02
  • Yes. (There are some other solutions, but it's the easiest one.) – HolyBlackCat Jul 05 '14 at 14:06
  • I just noticed the disadvantage of coding in native enviroment... the `#undef main` variant somehow works... but without adding `#undef main` `#include //#undef main int main( int argc, char* argv[] ) { return 0; }` compiles with errors again – Matze Jul 05 '14 at 14:17
  • I have to mention, that i can't find a statement like `#define main SDL_main` inside the `SDL.h`... I'm using SDL2-devel-2.0.3 – Matze Jul 05 '14 at 14:21
  • Remove that `#undef` code, you don't need it. >>> `i can't find a statement like #define main SDL_main` It's inside SDL_main.h. In SDL.h you can find `#include `. – HolyBlackCat Jul 05 '14 at 14:24
  • Now i'm getting more errors than ever before :D `Warning: corrupt .drectve at end of def file ` Full error: http://pastebin.com/S5EEaPzc – Matze Jul 05 '14 at 14:35
  • So, you can't compile file like this: `#include /*new line*/ int main(int, char **){}`? Create a new question about it. – HolyBlackCat Jul 05 '14 at 15:02
  • I can compile it with `g++ -c main.cpp -I SDL/include` but finally linking it with the dll causes the compiler to throw this error... but i'll do it, thanks for your support :) – Matze Jul 05 '14 at 16:33