简体   繁体   中英

Issues running WinMain in C

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.

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.

您需要在设置中将子系统从Console设置为Windows ,方法是在解决方案资源管理器中右键单击您的项目,选择properties ,然后转到Linker->System并将SubSystem设置为Windows(/SUBSYSTEM:WINDOWS)

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:

在此处输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM