-5

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:/filepath

LNK1120- 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

sss
  • 51
  • 8
  • You need to compile your project as a Windows application. I haven't used Windows or Visual Studio in so long I can't give you the precise names to look for, but check the solution options and look for something like application type. – Stephen Newell Jul 06 '23 at 04:23
  • 1
    _Unresolved symbol `main` referenced in function_ VSCode thinks you're trying to create a console/tty application. You want to compile the project as a GUI application. One of the dropdown/config menus will let you select this. _Side note:_ Normally, people [here, on SO] get the opposite: They _want_ a console/tty application, define `int main(int argc,char **argv)` and get unresolved `WinMain@16` – Craig Estey Jul 06 '23 at 05:12
  • There are tons of answered question on the matter here on SO. Please search for "WinMain" and read at least a dozen or so of them to learn that your project type is wrong... – the busybee Jul 06 '23 at 07:11
  • You can write windows programs in C or C++. If you are serious about using C, then [theForger's Win32 API Programming Tutorial](http://www.winprog.org/tutorial/start.html) is a good one. Though I would caution you -- you should not mix learning C with learning the Windows API at the same time. That is destined to end in failure. Both C and the Windows API are learning endeavors in and of themselves. Learn C first, then take the next step into the GUI world. Note, most GUI toolkits are C++ oriented as it lends itself well to GUI programming. – David C. Rankin Jul 06 '23 at 08:01
  • 1
    WinMain is the entry point, you cannot also use it as a CALLBACK – stark Jul 06 '23 at 11:19
  • Does this answer your question? [WINMAIN and main() in C++ (Extended)](https://stackoverflow.com/questions/13871617/winmain-and-main-in-c-extended) – Mgetz Jul 06 '23 at 13:52

1 Answers1

0

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)

sss
  • 51
  • 8