Problem: undefined reference to 'WinMain@16'

Started by
3 comments, last by CodeRot 10 years, 4 months ago

Hello,

I'm using two compilers: VS2005 Professional Edition and a pre-built distribution of the GCC toolchain obtained here. I'm using two compilers simply to test my code over alternative compilers. Also I'm a newbie to GCC.

I have a static library which contains the framework and implementation of the WinMain function and an executable which links in the static library. This works fine when I compile and link with the MS toolchain, however I get the error mentioned above when using the GCC toolchain. I know that this error is caused by the linker not finding the function in any of the object files that it is linking to the executable. Is there anyway of getting around this using either a command-line compilation and linkage (using GCC) or using Code::Blocks 10.05 to make the applicable compile and link settings to get this to work?.

Thanks.

Advertisement

I'm not a GCC/MinGW expert, but I would suggest that your linker settings may be in the wrong order, do you have -lmingw32 (or -lmingw64) listed ahead of other libraries (indicated by the -l prefix) or is it after a bunch of them?

Also worth a check, are you using the -mwindows flag? (I would guess you are as I wouldn't expect it to complain about WinMain otherwise).

I've checked the linker settings in Code::Blocks and neither of those libraries were present (at least I couldn't see if they were there). I added each one in turn (separately) and the linker reported that neither of the libraries were present on my system. I don't want to install these incase it messes up something else since I'm using someone elses GCC distribution and given that GCC is particularily picky when it comes to linking libraries and such.

As a test I created a standard Win32 GUI application and noted that it links in kernel32, user32 and gdi32 (same as my executable) there didn't appear to be any reference or link setting to either of the libraries that you mentioned. So I assume no problems there.

Anymore help appreciated.

I've checked the linker settings in Code::Blocks and neither of those libraries were present (at least I couldn't see if they were there). I added each one in turn (separately) and the linker reported that neither of the libraries were present on my system.

I'm not really familiar with C::B, when you added the lib did you put the -l prefix on? It's quite possible you need to link to the library differently in C::B than you would from the commandline (e.g. maybe with a fullblown path rather than the -l shorthand stuff). Or maybe it just doesn't know where the lib is (mis)located; there will be a way to add a lib directory if that's the case.

Can you find libmingw32.a in your mingw install? I believe this is a library you need to link with and to do so ahead of your custom lib containing WinMain.

My current hypothesis is this:
Your WinMain is in a separate lib, while the MS compiler is fine with that GCC is not because it will not link unused symbols and WinMain is, at that point, unused. The fix is to link with mingw32, a teeny(?) library that just has a regular main() function that forwards onto your WinMain function. You link it in ahead of your custom lib which therefore causes the linker to actually use your WinMain rather than ignore it.

My apologies. I've just double checked mingw32.a and it is in fact in my MinGW\lib installation directory. I checked the link line in C::B and I had "-lming32" not '-lmingw32' as it should have been. This now links and runs as it does with the MS toolchain.

Many thanks for your time and assistance. :)

This topic is closed to new replies.

Advertisement