undefined reference to `WinMain@16' collect2.exe: error: ld returned 1 exit status
Asked Answered
P

10

15

I am using eclipse CDT to test the Intel instructions and below is my program:

#define cpuid(func,ax,bx,cx,dx)\
__asm__ __volatile__ ("cpuid":\
 "=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx) : "a" (func));
int Check_CPU_support_AES()
 {
 unsigned int a,b,c,d;
 cpuid(1, a,b,c,d);
 return (c & 0x2000000);
 }

When I compile the above code, I get linkage error as:

Info: Internal Builder is used for build
gcc -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\Intel.o" "..\\src\\Intel.c" 
gcc -o Intel.exe "src\\Intel.o" 
c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status

Please help me regarding the issue.

Paleethnology answered 5/6, 2013 at 17:33 Comment(2)
This doesn't look like the entire program. Where is WinMain?Intelligence
He doesn't have a WinMain - that's why he gets the error he's getting.Pathway
P
16

Your program isn't complete. You need to implement the OS-expected entry point. In your case, that looks like it's called WinMain.

Pathway answered 5/6, 2013 at 17:41 Comment(1)
Yes, I figured it out. I need to implement main(). Thank You.Paleethnology
P
9

Yes, Main () function is missing and the compiler is not able to find an entry point for executing the program.

One more reason is even if you have written the main function but if you didnot save the .cpp file and try to compile it will give the same error.So make sure you have successfully saved the .cpp file and then compile and run your code.

Hope this will help since I have faced similar issue and I spent around hours to figure it out , Thanks

Poikilothermic answered 30/5, 2019 at 20:46 Comment(0)
C
6
  1. The main() function is missing.
  2. Save as this code as some new file. Again run to compile the code.
  3. Check the PATH environment variable.
Corollary answered 17/8, 2020 at 15:10 Comment(3)
I am not sure that this would be the cause. WinMain wants to be compiled in a GUI program, for console programs, there is only simple main().Maggoty
Somehow this worked for me, compiling file.cpp was giving me this error but when I copied the same code into another file index.cpp and compiled it. It worked just fine. My code was just the iostream header, main function and a cout statement for "hello world"Orta
Worked for me as well, Thanks, I guess just by saving the file it resolved the issue.Anaclinal
R
4

In VS Code, this can happen when you haven't saved your code. Click Ctrl + s to save the code and then run the program again.

Or to make this happen automatically, go to Settings and search for "save". Scroll down and search for "Whether to save the current file before running" and enable it.

Revamp answered 13/8, 2021 at 13:31 Comment(3)
@JanakaEkanayake - You significantly changed this answer. It was talking about some kind of auto-save option that automatically triggers before build / run, but you changed it to only talking about manually saving with a keyboard shortcut. Unless you know that there's no such feature, that's not a good edit. (The original could use some cleanup, but not changing the key point.)Shealy
@PeterCordes I agree with you thanks for the correctionLoft
This completely fixed my problem. I was confused seeing all these answers about things I was already doing. Apparently I just had to save the file before I could run it lol. Completely unhelpful error code as usual though.Behm
E
2

you have to save the file first>> Ctrl + s

Equivocation answered 22/9, 2021 at 1:21 Comment(2)
Multiple existing answers already say this, including one mentioning ctrl + s. Please don't post "me too" answers; upvote an existing answer that worked for you.Shealy
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.Scampi
C
1

If you already checked all the others reasons

I also got this message and it turned out that I accidentally placed my main() inside a namespace. So it was hidden for global access

Clabo answered 17/1, 2023 at 7:36 Comment(0)
A
0

Replace main() with main(int argc,char **argv), and it works for me.

Aarhus answered 17/5, 2021 at 15:59 Comment(1)
I'm guessing the args have to be exact so the @16 decoration (number of bytes to pop for __stdcall functions) will match what CRT is looking for. Callee-pops depends on the callee declaring its args exactly.Shealy
S
0

In VS code, save the file first

Schlicher answered 17/10, 2023 at 9:23 Comment(0)
H
0

When I had this error, it was because I forgot to actually add filename.

Hypercorrection answered 11/5, 2024 at 14:22 Comment(0)
W
0

It happen due to following reasons:

  1. If you have incorrect file name format:- In this case you can rename your filename and after that you can execute you code

  2. If your code don't have main() function of have incorrect spelling of main() function:- In this case correct your spelling and re-run your code

  3. Sometimes if your code is not saved:- In this case you can turn on your autosave mode or save the code before execution.

Woodnote answered 27/6, 2024 at 9:49 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.