Visual Studio Code“对‘WinMain@16’的未定义引用”

nic*_*ser 2 c++ mingw window visual-studio-code

所以我试图在 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);
}
Run Code Online (Sandbox Code Playgroud)

但是当我编译时,我收到此错误:

> 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
Run Code Online (Sandbox Code Playgroud)

我在.vscode 文件夹中还有一个tasks.json和一个launch.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
    }
  }
]
Run Code Online (Sandbox Code Playgroud)

启动文件

"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"
  }
]
Run Code Online (Sandbox Code Playgroud)

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

小智 9

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

  • 你真是个天才!我只是假设它像 PyCharm 一样自动保存。 (3认同)

小智 6

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


小智 1

WinMain@16通常在您尝试编译一些不包含该main()/WinMain()函数(程序的起点)的文件时出现。就您而言,不包含带有该main()/WinMain()函数的源文件会给您带来麻烦。