Visual Studio代码“未定义引用`WinMain @ 16'”

问题描述 投票:-2回答:1

所以我试图用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:

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

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-code window mingw
1个回答
0
投票

WinMain @ 16通常在您尝试编译某些文件时出现,这些文件不包含main()/WinMain()函数(程序的起点)。在你的情况下,不包括带有main()/WinMain()函数的源文件导致你的麻烦。

© www.soinside.com 2019 - 2024. All rights reserved.