Visual Studio Code “undefined reference to `WinMain@16'”

末鹿安然 提交于 2021-01-01 13:44:25

问题


so I am trying to make a Windows Desktop Application with c++ in Visual Studio Code and using MinGW as my compiler. I have a file called test.cpp in a folder called 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);
}

But when I compile I get this error:

> 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

I also have a tasks.json and a launch.json in .vscode folder:

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

The problem is that when I build a file with a main function it compiles fine, but when it is done with wWinMain that error happens and I don't know how to fix it. I'll really appreciate if someone can help me with this.


回答1:


I had the similar question, save the file manually and complie it again.




回答2:


Just include a main() function in your code and you will be good to go. It's just that your program doesn't know where to start.




回答3:


WinMain@16 usually appears when you try to compile some files, which doesn't contain the main()/WinMain() function (starting point of the program). In your case, not including the source file with the main()/WinMain() function in it was causing your troubles.



来源:https://stackoverflow.com/questions/53092183/visual-studio-code-undefined-reference-to-winmain16

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!