So… yesterday I was working a C program. I made one minor change to my code. I added a new function. I added the function in the .h file and the .c file. Then I started getting an error complaining of undefined reference to `WinMain@16′ on the console. I couldn’t figure out for the life of me what happened. After that, I kept consistently getting the error.
I read through stack overflow and found that question answered and while the answer right there in front of my eyes, I couldn’t understand it. Basically, the answer said that I didn’t have main properly defined. But I did. My main had the definition of int main(int argc, char* argv).
Note* If you came here looking because you are getting this error, check your code, you may be missing the main function or it’s not defined properly. You may be excluding the cpp fine that it’s defined in. In short, it just means that it cannot find main.
Well, this morning it did it again. This time I noticed that my main.c file had disappeared from my source code editor. As it turns out, I was typing my compile command wrong. I was leaving out the exe’s name in the command (see image).
By leaving out the main.exe from the command, the compile command was automatically deleting main.c (assuming that was my executable’s name). Then it would complain about the same error over and over again. Insanity I know. I tried changing my original files back to their original state but the error persisted.
The explanation to all this is that my main.c was gone and therefore all it was that I was missing the main function. Of course, because it had deleted main.c when it ate it up through the command.
Below is an image of how I wrongfully executed the compile command.
Luckily I had my code on Github, so I recovered main.c from GitHub and ran the compile command. This time I did correctly by adding the foo.exe into the command.
Hopefully, this helps someone out there. So if you get this undefined reference to
WinMain@16 error, just make sure that you have your main in one of the files that you are compiling and don’t do like I did by leaving out your .exe out of your command.
I should have probably named this post “My compiler ate my main.c file”. Thanks for reading. Until next time.