6

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.

4 Answers 4

24

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

Sign up to request clarification or add additional context in comments.

2 Comments

You're a genius! I was just assuming that it was auto-saving like PyCharm does.
can u tell me Please In my vs code errors not highlighted by red lines..(Earlier they are highlighted)
6

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.

Comments

2

Change the code runner setting “save file before run”.

Comments

1

WinMain@16 usually appears when you try to compile some files, which don'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.

2 Comments

Hi RistoS, so you ar esaying that I have to manually include the file where the WinMain function is included right? So in these case, that would be the window.h?
So what if you want to just build the one file out of your whole folder?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.