gpt4 book ai didi

c++ - 对 WinMain 的 undefined reference (C++ MinGW)

转载 作者:行者123 更新时间:2023-11-30 03:13:57 25 4
gpt4 key购买 nike

目前,我正在尝试使用 C++ 制作一个 Windows 应用程序。为了编译我的程序,我使用 MinGW (GCC)。但是,一旦我使用 int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) 而不是 int main() ,编译器就会向我显示以下消息:

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 应用程序教程中获得的:

Example Code

我已经尝试重新安装 MinGW,但没有帮助(而且我使用的是 Windows 10)。

最佳答案

此示例代码使用 wWinMain 但是

One thing to note is that Visual C++ supports a “wWinMain” entry pointwhere the “lpCmdLine” parameter is a “LPWSTR”. You would typically usethe “_tWinMain” preprocessor definition for your entry point anddeclare “LPTSTR lpCmdLine” so that you can easily support both ANSIand Unicode builds. However, the MinGW CRT startup library does notsupport wWinMain, so you’ll have to stick with the standard “WinMain”and use “GetCommandLine()” if you need to access command linearguments.

通过Building Win32 GUI Applications with MinGW

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

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

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

g++ other_options_and_arguments -municode

关于c++ - 对 WinMain 的 undefined reference (C++ MinGW),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58324230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com