3

any help here would be appreciated. Ive really racked my brains at this, sooo.

I have installed cygwin, and Netbeans and have been successfully deving, compiling and running a small SDL-1.2 windows game with no problems.

The problem has come now that I have installed SDL2 and am trying to compile. Specifically the linking.

Im doing the same as before, adding "libSDL2.a" and "libSDL2main.a" to the linker options for my project in Netbeans; but im getting the "undefined reference to `WinMain'" error

Now, ive looked into this and it seems that the linker cannot link my main() function to the WinMain one.

Also one answer ive seen is to add "-lmingw32 -lSDLmain -lSDL" to the linker options BUT I dont use mingw, im using cygwin, whats the cygwin equivalent of mingw32.lib

I guess the main question is: What are the options I give the linker if im using Cygwin, SDL2 and Netbeans?

any help would be greatly appreciated.

Tristus
  • 123
  • 1
  • 13
  • Linking on the command line is the same, no matter if you're using MinGW, Cygwin, native Linux terminal or even OSX. – Some programmer dude Apr 24 '15 at 12:01
  • kay, so. in cygwin when I run this "g++ main.cpp -lSDL2main -lSDL2" I get the exact same "undefined reference to `WinMain'". This is totaly seperate from netbeans also. – Tristus Apr 24 '15 at 12:20
  • In Cygwin and MinGW the libraries are built for POSIX environments (apparently), which means it doesn't have the Windows main function, and you need to create your own `WinMain` function which then calls the `main` function supplied by the `SDL2main` library. – Some programmer dude Apr 24 '15 at 12:28
  • hmm, okay so, is this new in SDL2? because i never needed to do anything with regards to a windows main with SDL1.2. Also, if this is all posix and I have no windows main in sight, then why is the linker complaining about WinMain at all? – Tristus Apr 24 '15 at 12:35

2 Answers2

8

Have you tested with an #undef main infront of your main?

/*
 * If 'main' is defined we clear that definition
 * to get our default 'main' function back.
 */
#ifdef main
# undef main
#endif /* main */

int main(int argc, char** argv)
{
    // ...
    return 0;
}

Using Netbeans with Cygwin and SDL, including SDL.h creates strange error


May also help:

I get "Undefined reference to 'WinMain@16'"

Under Visual C++, you need to link with SDL2main.lib. Under the gcc build environments including Dev-C++, you need to link with the output of "sdl-config --libs", which is usually: -lmingw32 -lSDL2main -lSDL2 -mwindows

( http://wiki.libsdl.org/FAQWindows#I_get_.22Undefined_reference_to_.27WinMain.4016.27.22 )

Community
  • 1
  • 1
ollo
  • 24,797
  • 14
  • 106
  • 155
  • okay so adding the #ifdef main #undef main #endif worked. However, i did read somewhere that this was a messy and nasty work around, I would feel better if I actually understood why this is and what it actually does? – Tristus Apr 24 '15 at 13:49
  • I guess it's because windows needs some additional setup. Best you have a look at the implementation of sdl main. – ollo Apr 28 '15 at 14:03
1

For a library that I just built with Cygwin, I added the following compiler flags:

 -shared -Wl,--out-implib,lib$(LIB_NAME).dll.a

This allowed me to build my library without the dreaded, "missing WinMain," error message.

Darwin Airola
  • 919
  • 8
  • 11