I’m trying to learn C, and I’m stuck right at the beginning. To my knowledge, WinMain is the entry point to a GUI application in C. I’m running into issues trying to get it to work. This is the code I’m trying to run
#include <windows.h>
void foo(void) {
OutputDebugStringA("This is the first thing we have actually printed.n");
}
int CALLBACK WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd
)
{
foo();
}
These are the corresponding errors
C29251- Inconsistent annotation for ‘WinMain’: this instance has no
annotations. See c:/filepathLNK1120- 1 unresolved externals
LNK2019- Unresolved symbol main referenced in function "int_cdecl
invoke_main(void)(?invoke_main@YAHXZ)
I’ve read through
https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-winmain
I’ve tried the two separate code snippets provided, and neither ran on my computer.
I’m using Visual Studio
2
Answers
The material I was looking at was a bit outdated. The problem was that I choose an empty project, thinking it was going to be the opposite of the console application, but it still required me to use a main function, so I figured it had to do with my build settings, so I changed that, and no luck, but what it actually was was the linker its self. The linker settings are a bit harder to find, you right-click on your project in the solution explorer and go to properties, or use the alt-enter shortcut, then you expand the linker tab, go to system, then change the subsystem from console to windows(/subsystem:windows)
See details at Walkthrough: Create a traditional Windows Desktop application (C++).
To start a Windows desktop application introduce what is WinMain.
If you want to use OutputDebugStringA() for debug output, please use DebugView , see at DebugView v4.90.
Like below picture:
Update
Walkthrough: Create a traditional Windows Desktop application (C++) archived at here.
To start a Windows desktop application archived at here.
DebugView v4.90 archived at here.
Important note: please use visual stdio 2022 to run code.
Demonstrate:please see my use at https://imgur.com/a/x7cbsxH