Home > OS >  having trouble with this error: undefined reference to `WinMain'
having trouble with this error: undefined reference to `WinMain'

Time:03-10

#include<stdio.h>

void main()
{
  printf("hello world");
}

After compiling the above program, its shows an error. So can anyone help me out with this?

the error is:

C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function main': C:/crossdev/src/mingw-w64-v8-git/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to WinMain' collect2.exe: error: ld returned 1 exit status

CodePudding user response:

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

CodePudding user response:

The error messages shown in your question indicate the Visual Studio environment is set to use TDM-GCC compiler.

"TDM-GCC is a compiler suite for Windows. It combines the most recent stable release of the GCC compiler, a few patches for Windows-friendliness, and the free and open-source MinGW.org or MinGW-w64 runtime APIs" (ref.)

The vanilla GCC part includes the -mconsole option to explicitly target a console application. Add this option into your compiler settings.

This option specifies that a console application is to be generated, by instructing the linker to set the PE header subsystem type required for console applications. This option is available for Cygwin and MinGW targets and is enabled by default on those targets, (but evidently not in your environment.)

More gcc options

  • Related