所以我试图在 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
}
}
]
启动.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 完成时,就会发生错误,我不知道如何修复它。如果有人可以帮助我,我将不胜感激。
原文由 nico.user 发布,翻译遵循 CC BY-SA 4.0 许可协议
更改代码运行器设置“运行前保存文件”。