Error: undefined reference to winmain@16?

I was trying to compile the following code in cygwin using g++:

------------------------------------------

#include <iostream>
using namespace std;

int identity(int input)
{
  int output = input;
  return output;
}

------------------------------------------

I get this error:

/usr/lib/gcc/i686-pc-cygwin/4.8.2/../../../libcygwin.a(libcmain.o): In function `main':
/usr/src/debug/cygwin-1.7.25-1/winsup/cygwin/lib/libcmain.c:39: undefined reference to `WinMain@16'
collect2: error: ld returned 1 exit status

Anyone know how I can solve this?

Yes. You are using cygwin. It compiles into what amounts to a Windows executable pretending it runs under Linux.

C requires int main() as a function in your code or it cannot link. ld is the linker.

main() will have to call your function.

I tried int main() and I still got errors. With int main(), the code looks like this:

#include <iostream>
using namespace std;


int main()

  int identity(int input) 
{
    int output = input;
    return output;
  
  
}

Now I am getting this error:

bh.cpp:9:3: error: expected initializer before �int�
   int identity(int input)
   ^

C does not require that you dump the words 'int main' into the middle of a program for no discernible reason, no.

It requires that a function named main() must exist, so that the program can begin there.

int main() {
        int output=identify(32);
}

I fixed it:

#include <iostream>

using namespace std;

int main()
{
  int identity(int input); 
    
    int output = input;

    return output;

}

But now I get this error:

bh.cpp: In function �int main()�:
bh.cpp:12:18: error: �input� was not declared in this scope
     int output = input;
                  ^

This is my first programming class, sorry if I am missing something obvious. My teacher tells me to find out what this program does. At school we use different operating system and a Unix terminal. I am using Cygwin on Windows vista.

Again, the compiler is not asking you to jam random things into the middle of your code when it says main doesn't exist. It's asking you to add main(), unrelated to your code, outside it.

Of course, if you want your function to be run, you should tell main() to run it, since main runs everything.

#include <iostream>

using namespace std;

// Your function
int identity(int input)
{
  int output = input;
  return output;
}

// The function which the compiler will call when the program is run
int main() {
        int output=identify(32);
}
1 Like

At first I got another error, but I realized you used "identify" instead of "identity", so that's fixed. Anyway, the output is nothing when I run ./bh.exe(the filename of this code). Is this correct?

It does nothing you can see from the outside, so yes, that is correct.

You would benefit from reading one of the many basic tutorials on getting started with C or C++. Based on your code I'd say look for C++ tutorials. I hope that is what your class is about.

1 Like