Page 1 of 1

SDL and WinMain() (GCC) [FIXED]

Posted: Sun Jun 03, 2007 8:39 am
by earlz
I have tried making a simple AI thingy and used SDL for a display, but I'd like to be able to use the console window...well I get an undefined reference to WinMain if I include mingw32, then this error disappears(and I can't just do -lSDL I have to do -lSDLmain and -lSDL.dll)

but if I don't have main() then I get undefined reference to SDL_main..so what the crap is with this!?

and then when I include all those libraries,(only iostream, cstdlib, and SDL.h are included) the console window will be there, but it won't let me print anything to it..I have tried printf, cout, and fprintf(stderr,"bah") but nothing works!

edit:
Well I figured out the console window problems came back to -lmingw32 and I fixed the undefined reference created by using this code

Code: Select all

#ifdef WIN32 //This will overcome the bugs with MingW and GCC
int main(int argc,char**v){}

//int SDL_main(int t,char **v){}

extern "C" int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow
)
#else
int main(int argc,char **argv)
#endif
{
//code follows..
not very pretty, but it works and is portable..
and my makefile command came down to

Code: Select all

	g++ $(_CFLAGS) -c main.cpp -o obj/main.o

	g++  $(_OBJS) -lSDL -o Robots_AI.exe
(ld doesn't work for some reason..it's like it doesn't link with the standard library cause I get undefined symbols for strings and cout and such...)

Posted: Sun Jun 03, 2007 12:22 pm
by jhawthorn
To fix "Undefined reference to WinMain" you should have used -mwindows.