对WinMain的未定义引用(C ++ Mingw) [英] Undefined reference to WinMain (C++ Mingw)

查看:393
本文介绍了对WinMain的未定义引用(C ++ Mingw)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在尝试使用c ++制作Windows应用程序.为了编译程序,我使用Mingw GCC.顺便说一句,我在Windows 10上.但是,一旦我使用 int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE,PWSTR pCmdLine,int nCmdShow)而不是 int main(),编译器就会向我显示以下消息:

currently I am trying to make a windows application using c++. For compiling my program I use Mingw GCC. Btw I'm on Windows 10. But as soon as I use int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) instead of int main() the compiler shows me following message:

C:/mingw-w64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e):对"WinMain" collect2.exe的未定义引用:错误:ld返回1退出状态终端进程终止,退出代码为:1

C:/mingw-w64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain' collect2.exe: error: ld returned 1 exit status The terminal process terminated with exit code: 1

我尝试从此Windows应用程序教程中编译的示例代码:示例代码

The example code I tried to compile I got from this Windows application tutorial: Example Code

我已经尝试过重新安装mingw,但这没有帮助.这就是为什么我希望这里有人可以帮助我解决我的问题.预先谢谢你!

I have already tried reinstalling mingw but it did not help. That's why i hoped someone here could help me with my problem. Thank you in advance!

推荐答案

此示例代码使用 wWinMain ,但是

要注意的一件事是Visual C ++支持"wWinMain"入口点其中"lpCmdLine"参数为"LPWSTR".您通常会使用入口点的"_tWinMain"预处理程序定义,以及声明"LPTSTR lpCmdLine",以便您可以轻松地支持两个ANSI和Unicode版本.但是,MinGW CRT启动库不支持wWinMain,因此您必须坚持使用标准的"WinMain"并在需要访问命令行时使用"GetCommandLine()"争论.

One thing to note is that Visual C++ supports a "wWinMain" entry point where the "lpCmdLine" parameter is a "LPWSTR". You would typically use the "_tWinMain" preprocessor definition for your entry point and declare "LPTSTR lpCmdLine" so that you can easily support both ANSI and Unicode builds. However, the MinGW CRT startup library does not support wWinMain, so you’ll have to stick with the standard "WinMain" and use "GetCommandLine()" if you need to access command line arguments.

通过使用MinGW构建Win32 GUI应用程序

在这种情况下,您可以改用 WinMain .该程序不使用 pCmdLine 值,因此,当您将 wWinMain 更改为 WinMain PWSTR pCmdLine 时,它应该可以编译到 PSTR pCmdLine .

In this specific case, you can use WinMain instead. This program doesn't use pCmdLine value, so it should compile when you change wWinMain to WinMain and PWSTR pCmdLine to PSTR pCmdLine.

如果以后需要unicode命令行,请使用 LPWSTR cmd_line = GetCommandLineW(); 而不是 WinMain 参数.

If you later would need unicode command line use LPWSTR cmd_line = GetCommandLineW(); instead of WinMain argument.

较新的Mingw版本还支持将 -municode 链接器选项切换到备用启动代码,从而允许使用 wWinMain 而不是 WinMain (或wmain 而不是 main ).将其添加到命令行,IDE或makefile中的链接器选项.

Newer Mingw versions also support -municode linker option switching to alternate startup code allowing to use wWinMain instead of WinMain (or wmain instead of main). Add it to your command line, linker options in IDE or makefile.

g++ other_options_and_arguments -municode

这篇关于对WinMain的未定义引用(C ++ Mingw)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆