annunarcist
annunarcist

Reputation: 1805

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

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.

Upvotes: 16

Views: 114514

Answers (10)

Lokesh Singh
Lokesh Singh

Reputation: 1

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.

Upvotes: 0

Ray
Ray

Reputation: 241

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

Upvotes: 0

Sakib Shaikh
Sakib Shaikh

Reputation: 17

In VS code, save the file first

Upvotes: -1

Thomas Kintzel
Thomas Kintzel

Reputation: 74

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

Upvotes: 1

Mahmoud Hamad
Mahmoud Hamad

Reputation: 29

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

Upvotes: 1

Captain
Captain

Reputation: 51

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.

Upvotes: 4

Shreya
Shreya

Reputation: 1

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

Upvotes: 0

Rakesh
Rakesh

Reputation: 71

  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.

Upvotes: 6

Chandra Shekhar
Chandra Shekhar

Reputation: 617

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

Upvotes: 9

Carl Norum
Carl Norum

Reputation: 225202

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

Upvotes: 18

Related Questions