Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code "undefined reference to `WinMain@16'"

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.

like image 352
nico.user Avatar asked Nov 29 '22 08:11

nico.user


2 Answers

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

like image 177
junpfeng Avatar answered Dec 05 '22 17:12

junpfeng


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.

like image 27
Shuvam Kumar Panda Avatar answered Dec 05 '22 18:12

Shuvam Kumar Panda