Jitesh Yadav
Jitesh Yadav

Reputation: 57

Undefined reference to `WinMain@16' while compiling

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

Upvotes: 4

Views: 48592

Answers (8)

gamerfan82
gamerfan82

Reputation: 43

Just add this to the end of your code:

int main(){}

The problem is solved :)

Upvotes: 1

Boharsteve
Boharsteve

Reputation: 31

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.

Upvotes: 3

Nikhil Verma
Nikhil Verma

Reputation: 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.

Upvotes: -1

Ethan N
Ethan N

Reputation: 23

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.

Upvotes: 1

Raghav
Raghav

Reputation: 343

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.

Upvotes: 8

Saurav singh
Saurav singh

Reputation: 25

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

Upvotes: 0

junpfeng
junpfeng

Reputation: 271

Save your single file and then it will compile successfully.

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

Upvotes: 2

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

Upvotes: 0

Related Questions