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

53,932

Solution 1

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

Solution 2

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

Solution 3

  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.

Solution 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.

Solution 5

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

Share:
53,932
annunarcist
Author by

annunarcist

I enjoy learning/reading/watching anything that is new - be it about computer science concepts or history or politics or movies etc.

Updated on September 22, 2021

Comments

  • annunarcist
    annunarcist over 2 years

    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.

  • annunarcist
    annunarcist almost 11 years
    Yes, I figured it out. I need to implement main(). Thank You.
  • peterh
    peterh over 3 years
    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().
  • Raahim Fareed
    Raahim Fareed over 3 years
    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"
  • Peter Cordes
    Peter Cordes almost 3 years
    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.
  • Zircoz
    Zircoz almost 3 years
    Your innocent answer is underestimated xD
  • Peter Cordes
    Peter Cordes over 2 years
    @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.)
  • INDRAJITH EKANAYAKE
    INDRAJITH EKANAYAKE over 2 years
    @PeterCordes I agree with you thanks for the correction
  • Peter Cordes
    Peter Cordes over 2 years
    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.
  • Admin
    Admin over 2 years
    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.