Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML Main  (Read 10433 times)

0 Members and 1 Guest are viewing this topic.

CodeCriminal

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
SFML Main
« 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

Code: [Select]
#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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SFML Main
« Reply #1 on: October 11, 2009, 11:42:53 pm »
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?
Laurent Gomila - SFML developer

CodeCriminal

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
SFML Main
« Reply #2 on: October 12, 2009, 12:35:30 am »
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
Code: [Select]
#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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SFML Main
« Reply #3 on: October 12, 2009, 08:05:39 am »
What if you put it in another .cpp file?
Laurent Gomila - SFML developer

CodeCriminal

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
SFML Main
« Reply #4 on: October 12, 2009, 07:17:31 pm »
Quote from: "Laurent"
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.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
SFML Main
« Reply #5 on: October 14, 2009, 02:25:56 am »
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 use the latest build of SFML2

CodeCriminal

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
SFML Main
« Reply #6 on: October 15, 2009, 03:14:30 am »
Quote from: "OniLink10"
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..

dunce

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
SFML Main
« Reply #7 on: October 15, 2009, 10:03:39 am »
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:

Quote
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:

 
Code: [Select]
int main(bool blabla)
{
  return 0;
}


The compiler will be happy and the exe will work successfully.
 :wink:

Toucan

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: SFML Main
« Reply #8 on: May 05, 2019, 06:50:53 pm »
I was able to solve this problem by following the instructions in this post on MSDN 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.

 

anything