Visual Studio Code "undefined reference to `WinMain@16'"

Visual Studio Code "undefined reference to `WinMain@16'"

本文关键字:WinMain@16 reference Studio Code undefined Visual to      更新时间:2023-10-16

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

#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

tasks.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
}
}
]

启动.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完成时,会发生错误,我不知道如何修复它。如果有人能帮我,我将不胜感激。

我也有类似的问题,手动保存文件并重新编译。

只要在代码中包含一个main()函数,就可以开始了。只是你的程序不知道从哪里开始。

更改代码运行程序设置"运行前保存文件"。

WinMain@16通常在您试图编译一些不包含main()/WinMain()函数(程序的起点(的文件时出现。在您的情况下,没有包含包含main()/WinMain()函数的源文件会导致您的问题。