【问题标题】:Visual Studio Code "undefined reference to `WinMain@16'"Visual Studio Code“未定义对‘WinMain@16’的引用”
【发布时间】:2019-04-05 03:22:21
【问题描述】:

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

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

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】:

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

    【讨论】:

    • 你是个天才!我只是假设它像 PyCharm 一样自动保存。
    • 你能告诉我吗,请在我的 vs 代码中,红线未突出显示错误..(之前它们被突出显示)
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      更改代码运行器设置“运行前保存文件”。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 嗨 RistoS,所以你说我必须手动包含包含 WinMain 函数的文件,对吗?所以在这种情况下,那将是 window.h?
        猜你喜欢
        • 2021-11-18
        • 2013-01-15
        相关资源
        最近更新 更多