undefined reference to `WinMain' 🐞

undefined reference to `WinMain' 🐞

Visual Studio Code solution

You have just completed your first simple piece of C++ code and, lo and behold, you come across your first error or bug! This guide will show how I fixed the error 'undefined reference to WinMain' in Visual Studio Code (VS Code).

Below is a simple piece of code that should print "Hello World" when run.

This was the output instead 🐞!!

The following steps will only work if the G++ Compiler has been installed and its path has been added to Environment variables in the System Properties. You can navigate to MSYS2 and follow the instructions for installing G++ Compiler if you have not already done so. Should you have done all that and the error persists (as in my case), the next step may be to verify whether VS Code is configured to run C++ code efficiently. Here are the steps that I took to resolve the error:

Step 1: Inside VS Code click on the settings ⚙️ icon and select Settings.

Step 2: In Settings, we need to check off a few items. Start by typing save in the search bar and selecting "Save File Before Run" as shown in the screenshot.

The above is an illustration of where to select save file before run.

Step 3: In the search bar again, type code runner and select “Clear Previous Output“ and “Run In Terminal“ as shown in the screenshot below.

The above is an illustration of where to select to clear previous output and run in terminal.

Step 4: Close Settings and click on Run (Ctrl+ Alt + N) to compile your code again.

Step 5: Success!

The above is an illustration of a successful compilation.

Hopefully, this guide has assisted you in resolving this specific issue or helped to point you in the direction of the solution specific to your need.

Thank you for reading. Happy coding!