在C中运行Winmain的问题
目前,我在C中(特别是在Visual Studio中)中遇到Winmain
。
例如...
#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 ==========
仅此基本设置就给了我“ Winmain的不一致的注释”警告。我一直在寻找2天的帮助,而我最接近答案的是人们在C ++的背景下谈论Winmain。我觉得这是Visual Studio的问题,因为我最初只是使用VS代码,并设法获得了一个应用程序(生成弹出窗口的应用程序)进行编译和运行。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要使用
Winmain
程序输入点,您需要告诉链接器以定位“ Windows”子系统,而不是构建控制台应用程序。否则,链接器将寻找(并且找不到)C程序的标准Main
输入点。在解决方案资源管理器中,右键单击您的项目,然后选择“属性”。然后导航到“链接器 - &gt; system”页面,然后选择“ Windows(/Subsystem:Windows)”为目标:
在您的“不一致的注释”警告中,请参阅此q/a:< a href =“ https://stackoverflow.com/q/47958366/10871073”>'winmain''不一致的注释
另外,正如所指出的在SNO 的答案中,您应该将
Winapi
属性添加到您的Winmain
函数。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 standardmain
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 yourWinMain
function.您只是将Winmain设置为错误。这应该有效:
或
不要忘记#include&lt; windows.h&gt;并将子系统更改为Windows。
You just set up your winmain wrong. This should work:
or
Don't forget to #include <Windows.h> and change subsystem to windows.
您需要将子系统从
Console
设置为Windows
在设置中,右键单击“解决方案资源管理器”中的项目,选择properties
,然后转到<<<代码>链接器 - &gt; System 和设置子系统
toWindows(/Subsystem:Windows)
。You need to set subsystem from
Console
toWindows
in settings, by right clicking your project in the solution explorer, selectingproperties
, and going toLinker->System
and settingSubSystem
toWindows(/SUBSYSTEM:WINDOWS)
.