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.

4 Answers

I had the similar question, save the file manually and compile 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.

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

0

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

You Might Also Like