Home > Mobile >  Issues running WinMain in C
Issues running WinMain in C

Time:06-15

Currently I'm having issues with WinMain in C (specifically in Visual Studio).

For instance...

#include <stdio.h> 
#include <Windows.h>

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    PSTR lpCmdLine, INT nCmdShow)
{

    return(0);
}

1>------ Build started: Project: GameB, Configuration: Debug x64 ------
1>LIBCMTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
1>E:\James\VisualStudio\CProjects\GameB\x64\Debug\GameB.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "GameB.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Just this basic set-up gives me an "inconsistent annotation for WinMain" warning. I have been searching for any help for 2 days and the the closest I come to an answer is people talking about WinMain in the context of C . I have a feeling this is a problem with Visual Studio as I was originally just using VS Code and managed to get an app (one that generated a pop-up window) to compile and run.

CodePudding user response:

You just set up your winmain wrong. This should work:

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow);

or

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PSTR lpCmdLine, INT nCmdShow)
{
    return 0;
}

Don't forget to #include <Windows.h> and change subsystem to windows.

CodePudding user response:

To use the WinMain program entry point, you need to tell the linker to target the "Windows" subsystem, rather than building a console application. Otherwise, the linker will look for (and fail to find) the standard main entry point for C programs.

In the Solution Explorer, right-click on your project and select "Properties". Then navigate to the "Linker -> System" page and select "Windows (/SUBSYSTEM:WINDOWS)" as the target:

enter image description here

On your "inconsistent annotation" warning, see this Q/A: Inconsistent annotation for 'WinMain'


Also, as noted in the answer by SNO, you should add the WINAPI attribute to your WinMain function.

CodePudding user response:

You need to set subsystem from Console to Windows in settings, by right clicking your project in the solution explorer, selecting properties, and going to Linker->System and setting SubSystem to Windows(/SUBSYSTEM:WINDOWS).

  • Related