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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
annunarcist
  • 1,637
  • 3
  • 20
  • 42

7 Answers7

14

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

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
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

Chandra Shekhar
  • 598
  • 1
  • 7
  • 25
5
  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.
peterh
  • 11,875
  • 18
  • 85
  • 108
Rakesh
  • 61
  • 1
  • 1
  • 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()`. – peterh Aug 17 '20 at 15:44
  • 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" – Raahim Fareed Jan 01 '21 at 07:40
  • Worked for me as well, Thanks, I guess just by saving the file it resolved the issue. – Trishant Saxena Aug 28 '23 at 13:13
2

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Captain
  • 31
  • 1
  • @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.) – Peter Cordes Aug 21 '21 at 13:41
  • @PeterCordes I agree with you thanks for the correction – INDRAJITH EKANAYAKE Aug 21 '21 at 14:03
2

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

  • 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. – Peter Cordes Sep 22 '21 at 01:25
  • 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 22 '21 at 03:44
0

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

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Shreya
  • 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. – Peter Cordes May 17 '21 at 19:46
0

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