0

I have been trying to create a basic application (in Visual Studio 2019, not Visual Studio Code) that would consist of a simple message box. I used the empty project template, and have nothing but the following file in the project:

//main.cpp
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
    int nShowCmd) {

    MessageBox(NULL, L"Goodman", L"Saul", 0);
    return 0;
}

Much to my frustration, upon building the solution, it throws a LNK2019 error:

unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)

I have looked online and all I could find were posts such as this one, which suggest changing the subsystem, which does not resolve the error.

Is there any way to fix this error?

44a3
  • 35
  • 4
  • 2
    Does this answer your question? [Why is my Windows program trying to call main() instead of WinMain()?](https://stackoverflow.com/questions/10901825/why-is-my-windows-program-trying-to-call-main-instead-of-winmain). Also see [https://stackoverflow.com/q/12573816/62576] – Ken White May 16 '23 at 02:15
  • @KenWhite No, I have tried to change the subsystem to `Windows (/SUBSYSTEM:WINDOWS)`, and the issue persists. – 44a3 May 16 '23 at 02:20
  • 3
    If you're using Visual Studio, not Visual Studio Code, then changing the subsystem will work. You do need to make sure that the platform you are changing it for is the same as what you are compiling. Best to select all for both platform and debug/release at the top first. – Retired Ninja May 16 '23 at 02:20
  • 1
    The first post I linked is correct - it's the only thing that will cause the problem you're having. `main` is for console apps, `WinMain` for Windows apps. The problem is that the linker is trying to create a console app and isn't finding the proper main function. You apparently have not properly changed the subsystem. – Ken White May 16 '23 at 02:25
  • @KenWhite Sincerest apologies, I just checked the platform, and it was set to only x64, instead of all configurations (Thanks @RetiredNinja). I think I am going to step away from my PC for a long while now. – 44a3 May 16 '23 at 02:28
  • 1
    @44a3 I've been using Visual Studio almost daily since version 1.5 and I still forget to check if I'm changing the right configuration sometimes. :) – Retired Ninja May 16 '23 at 02:31

0 Answers0