gpt4 book ai didi

c++ - Visual Studio 代码 "undefined reference to ` WinMain@1 6'"

转载 作者:行者123 更新时间:2023-12-04 14:29:27 26 4
gpt4 key购买 nike

所以我试图在 Visual Studio Code 中使用 C++ 制作一个 Windows 桌面应用程序,并使用 MinGW 作为我的编译器。
我在名为 src 的文件夹中有一个名为 test.cpp 的文件:

#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>

int wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine,
int nCmdShow){

const wchar_t name[] = L"Test";

WNDCLASS wc = {};
//wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = name;

RegisterClass(&wc);

HWND hWnd = CreateWindowEx(
0,
name,
L"Window",
WS_BORDER,
CW_USEDEFAULT,
CW_USEDEFAULT,
1200,
720,
0,
0,
hInstance,
0);

if(hWnd == NULL){
return 0;
}

ShowWindow(hWnd, nCmdShow);
}

但是当我编译时出现这个错误:
> Executing task: g++ -g test.cpp -o test.exe <

c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
The terminal process terminated with exit code: 1

我在 .vscode 文件夹中还有一个 tasks.json 和一个 launch.json:

任务.json
"version": "2.0.0",
"tasks": [
{
"label": "test",
"type": "shell",
"command": "g++",
"options": {
"cwd": "${workspaceFolder}/src"
},
"args": [
"-g", "test.cpp", "-o", "test.exe"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]

Launch.json
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/src/test.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/src",
"environment": [],
"externalConsole": true,
"preLaunchTask": "test"
}
]

问题是,当我用 main 函数构建一个文件时,它编译得很好,但是当它用 wWinMain 完成时,错误发生了,我不知道如何修复它。如果有人可以帮助我,我将不胜感激。

最佳答案

我有类似的问题,手动保存文件并再次编译。

关于c++ - Visual Studio 代码 "undefined reference to ` WinMain@1 6'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53092183/

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