SFML community forums
General => General discussions => Topic started by: CodeCriminal on October 11, 2009, 09:52:30 pm
-
Hi, this question is mostly directed at Laurent.
I was wondering how these lines actually work or rather why they work when placed in a static library
#if defined(_WIN32)
#include <windows.h>
extern int main(int argc, char* argv[]);
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
{
return main(__argc, __argv);
}
#endif // _WIN32
I tried this myself in the same .cpp as i was calling main but i get an error telling me that main cannot be overloaded. I would just like an explanation please :)
Thanks
-
There's nothing to explain, the weird thing is that you got an error when you tried to use is directly :)
Did you create a "Win32 application" project (not a "console application")? Can you show your code?
-
Yes i created a windows application and checked that it was under linker sub-system
odd...
It works when linking to your static library and i checked the the project in the build folder, just to make sure i wasnt missing anything.
so here is the code, as i said i just slapped it into the main .cpp
#if defined(_WIN32)
#include <windows.h>
extern int main( int argc, char* argv[] );
INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, INT )
{
return main( __argc, __argv );
}
#endif
int main( )
{
return 0;
}
But it works if i just link your static library without changing my entry point atall.
-
What if you put it in another .cpp file?
-
What if you put it in another .cpp file?
Hmmm, that seems to work, although i do still find it rather perculier.
As far as i know it should not make any difference.
-
I think the problem might be the "extern" in the main declaration. Put all of that code after your main function and then remove the "extern int main( int argc, char* argv[] );" line.
-
I think the problem might be the "extern" in the main declaration. Put all of that code after your main function and then remove the "extern int main( int argc, char* argv[] );" line.
Already tried that, doesnt work either, also just tested again and no luck.
I also tried putting the lines in a DLL and exporting them but it doesnt seem to work.. I kind of know why it doesnt work but cant put my finger on it..
-
If you place your main() function with a different argument list in the same cpp file with previously declared main() the compiler considers the second main() as the function overloading. But main(), WinMain()... cannot be overloaded. This is what the MSDN says:
The functions main, WinMain, DllMain, and LibMain cannot be overloaded.
The following sample generates C2731:
// C2731.cpp
extern "C" void WinMain(int, char *, char *);
void WinMain(int, short, char *, char*); // C2731
The question arises: why it works when you move your main() to another file? IMO it works because the real argument list of main() does not matter at all. For example you may define your main function in another cpp file this way:
int main(bool blabla)
{
return 0;
}
The compiler will be happy and the exe will work successfully.
:wink:
-
I was able to solve this problem by following the instructions in this post on MSDN (https://social.msdn.microsoft.com/Forums/en-US/c1c398dd-f30e-403b-81b4-f1eb9f5828b1/unresolved-external-symbol-wwinmain-referenced-in-function-wwinmaincrtstartup?forum=vssmartdevicesnative) in combination with adding WinMain with the extern reference to main shown in answers above.
"Look out for the Entry point settings in linker section. As for other MFC based projects with MFC 8.0, I think it should be WinMainCRTStartup instead of wWinMainCRTStartup." My Entry point option was initially blank, but setting it to WinMainCRTStartup worked!
Admittedly, I may have created my project as the wrong type. If you think that may be the case, this should fix your problem.