Help : Visual Studio - Intro to C on Windows

I recently started the Intro to C videos and I am not able to run the following code on Visual Studio 2019 (Version 16.4.5).

As there is no Win32 option in the start menu, I created an empty desktop application using the Windows Desktop Wizard (as in here)

Code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <windows.h>

int CALLBACK WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR     lpCmdLine,
    int       nCmdShow
);

void foo(void)
{

}


Error:
1
2
3
4
5
6
1>------ Build started: Project: intro_c, Configuration: Debug Win32 ------
1>Source.cpp
1>MSVCRTD.lib(exe_winmain.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
1>C:\Users\HP\source\repos\intro_c\Debug\intro_c.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "intro_c.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Also, if I include
1
<iostream>
and write a hello world program along with the above code, the code runs fine.
I searched every error and looked all over the internet, but couldn't find the solution.
What is the problem here ? Have I messed up during creating a new project? Please help.

Edited by sathvik on Reason: Initial post
You did not implement the WinMain function. So compiler is asking you to provide implementation.

You need to write code inside WinMain function, like this:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int CALLBACK WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR     lpCmdLine,
    int       nCmdShow)
{

 /// .. your code goes here

}

Check your project config.

Right-click the project -> Properties -> Linker -> System -> SubSystem.

You probably want that to be set to Windows (/SUBSYSTEM:WINDOWS)

=== tried that, worked for me, VS 2017; got my answer from https://stackoverflow.com/questions/49246136/unresolved-external-int-cdecl-invoke-mainvoid-after-vs2017-15-6-1-update

Linker subsystem is not the problem here. The problem was that there is no definition provided for WinMain, only declaration.

indeed, from the message >>_WinMain@16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)

it seems like they are trying to use invoke_main (tho it’s a guess) which is for console applications

all in all, op and i get that message when we followed day 1 cuz we chose console application, we should’ve chosen the windows desktop application (corresponding to win32 app in the video)

No, that's wrong - they are not calling invoke_main. invoke_main is MSVC runtime internal function that calls either [w]main or [w]WinMain depending on subsystem you set. It is trying to call WinMain because subsystem is already set to WinMain.

If you would create CONSOLE subsystem project and not provide main function, then you would get different error message - about unresolved external symbol main referenced in invoke_main function.

But because error message says that invoke_main is referencing unresolved WinMain symbol that 100% means subsystem is already set to WINDOWS.


Edited by Mārtiņš Možeiko on
Replying to pulsecch (#25723)