-1

I started my Visual Studio project as a windows application, however I've come to realize that if I want to use GLFW then I'm supposed to open a GLFW window instead of a standard wWinMain window. I have a wWinMain function but since it kept running every time I ran the program instead of my int main() function with the GLFW window test code inside, I changed the name of the wWinMain function in the hopes that when building the program it would defer to the main() function I wrote. However it hasn't worked and instead I keep getting the same error:

error LNK2019: unresolved external symbol WinMain referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)

How do I get it to stop looking for the wWinMain function and just run the main() one instead?

The only solution that has worked so far is to rename my main() function to wWinMain and have it accept all the variables but do nothing with them and just run my code as normal inside, however this doesn't seem optimal.

I've also tried the answer suggested here but that always opens a command window with the GLFW window whereas with the wWinMain function by default would run without one except when I specifically used AllocConsole(), so I suspect the answer suggested there isn't actually the correct solution for my issue.

Ive also tried the solution shown here but that just doesn't work for me? idk maybe I'm implementing it wrong

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • 3
    `wWinMain` doesn't have to create any WIN32 windows. If it's doing so then there must be some code in there to do it and you can just take it out. Then do whatever GLFW magic you have in mind instead. – Paul Sanders May 19 '22 at 23:08
  • @PaulSanders no I know why wWinMain is opening the console (I have an AllocConsole call within the wWinMain function), the issue is that the console opens even when wWinMain *isn't called* which I believe as indicative of some issue with that solution or my implementation – Fez_Master May 19 '22 at 23:11
  • 2
    Sounds like you need to create a "Windows Desktop Application" project rather than a "Console App" project. – Paul Sanders May 19 '22 at 23:14

3 Answers3

3

Windows knows two types of program that are relevant to you: Console and graphical.

Console programs automatically get a console (and you can get their output in a command line, for example) and their entry point is main or wmain.

Graphical programs don't get a console automatically, which is what you want. They don't automatically create any windows either; you'll have to do that manually (i.e. with GLFW like you want to). Their entry point is always WinMain or wWinMain.

You'll have to choose one, you can't mix-and-match. Just put your GLFW code in wWinMain. main is meaningless in a graphical Windows program, you should remove it. There's nothing wrong with not using the parameters passed to wWinMain - if you don't need them, that's fine. main also takes parameters that you don't currently use (but main is a little special since you can omit the unused parameters).

Jonathan S.
  • 1,796
  • 5
  • 14
  • 1
    To be pedantic; WinMain is not the the PE entry point. The real entry point calls your WinMain function after it has initialized your globals etc. – Anders May 20 '22 at 01:23
  • 1
    As always, Raymond Chen has this covered: See [The name WinMain is just a convention](https://devblogs.microsoft.com/oldnewthing/20061204-01/?p=28853) and [WinMain is just the conventional name for the Win32 process entry point](https://devblogs.microsoft.com/oldnewthing/20110525-00/?p=10573). – IInspectable May 21 '22 at 10:41
3

The choice of main or winMain is controlled in the linker section of the project

enter image description here

  • Subsystem = Console means main
  • Subsystem = windows means winmain
pm100
  • 48,078
  • 23
  • 82
  • 145
  • And of course whether you get the regular entry point or the `w` version depends on if you've selected the Ansi character set or Unicode. I definitely recommend Unicode. – Mark Ransom May 20 '22 at 00:12
0

I would just add

int WinMain() {
    return main();
}

after your main() function. This way, if you switch between the Windows subsystem and the Console subsystem, it will work both ways.

  • It's undefined behavior to ever call `main` directly in C++; this can only ever be called safely by the operating system. – Human-Compiler Jul 23 '22 at 21:01
  • If you had the Windows SubSystem enabled, WinMain would be the main function instead of main() –  Jul 24 '22 at 04:49
  • That doesn't stop it from being undefined behavior as is specified in the C++ standard. The compiler can legally optimize out the whole call to `main` because it's simply not legal to make that call, ever. The calling convention to `main` is not standardized; only the implementation knows how to do that. There's no guarantee that it will be the same as a normal function. – Human-Compiler Jul 25 '22 at 14:20
  • ohhhhhhhhhhh ok –  Jul 27 '22 at 15:11