5

I just installed Visual Studio Code and was trying to run my code when this came up:

Undefined reference to `WinMain@16' while compiling

I searched through the web for a relevant answer, but none that I found worked.

Here is a more detailed output in the console:

cd "f:\" && g++ testing.cpp -o testing && "f:\"testing
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,

By the way, I am using code-runner to run that single file. I have MinGW path all set up—though I don't know if that even matters.

here is what it looks like

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Jitesh Yadav
  • 51
  • 1
  • 1
  • 3
  • I am not trying something very complex. Just a hello world program for now to test the proper working of the VS Code. – Jitesh Yadav Oct 28 '18 at 21:36
  • Did you configure the mingw environment correctly? – Biswapriyo Oct 29 '18 at 08:00
  • Does this answer your question? [Why don't other programs see the changes I made to a file in VS Code until I save those changes?](https://stackoverflow.com/questions/76984829/why-dont-other-programs-see-the-changes-i-made-to-a-file-in-vs-code-until-i-sav) – starball Aug 27 '23 at 06:07

8 Answers8

8

The code runner does not save your code before running it. You can see the commands it executes in the error snippet you have added

cd "f:\" && g++ testing.cpp -o testing && "f:\"testing

So make sure you save your code before running it. This happened with me and I wasted quite some time.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Raghav
  • 323
  • 1
  • 3
  • 10
2

Save your single file and then it will compile successfully.

I had the same question as yours and I solved it this way.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
junpfeng
  • 271
  • 3
  • 3
2

I had the same problem and this worked for me: In Visual Studio Code, go to:

File -> Preferences -> Settings -> Run Code Configuration

And check the Save all files before run.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Boharsteve
  • 21
  • 1
0

If it is cpp file, you should have a main function in your file to avoid this error.

0

I am guessing you are using some library such as SDL2. The error is saying that it cannot find the entry point to your program.

undefined reference to `WinMain@16'

That is because your library redefined what the entry point function is called.

Instead of using:

int main() {
    std::cout << "Hello World" << std::endl;
    return 0;
}

you should use:

int WinMain() {
    std::cout << "Hello World" << std::endl;
    return 0;
}

Of course, this should only be used for testing that everything is being included correctly. After you start actually using the library, it will probably have some init function that will call the WinMain() function for you.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Ethan N
  • 13
  • 1
  • 5
0

Just go to file>Autosave. just click on autosave and it should solve your issue

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 19 '23 at 21:29
0

Just add this to the end of your code:

int main(){}

The problem is solved :)

gamerfan82
  • 33
  • 5
-1

You need to save the program before executing, as @junpfeng noted; that's the most probable problem.

If the problem still persists then that means your Mingw folder got corrupted and you need to reinstall it. It won't take long, just 3-4 minutes max.

In case you want the link: https://sourceforge.net/projects/mingw/

P.S., try clearing the path again in the environment variable.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 13 '21 at 16:00