在C中运行Winmain的问题

发布于 2025-02-07 20:08:28 字数 888 浏览 1 评论 0原文

目前,我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萤火眠眠 2025-02-14 20:08:28

要使用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 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.

記柔刀 2025-02-14 20:08:28

您只是将Winmain设置为错误。这应该有效:

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

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

不要忘记#include&lt; windows.h&gt;并将子系统更改为Windows。

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.

泛滥成性 2025-02-14 20:08:28

您需要将子系统从Console设置为Windows在设置中,右键单击“解决方案资源管理器”中的项目,选择properties,然后转到<<<代码>链接器 - &gt; System 和设置子系统 to Windows(/Subsystem:Windows)

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文