Undefined reference to WinMain in Cygwin
Asked Answered
F

1

10

I am trying to compile and having following problem

$ gcc errlib.c -o errlib.o

/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/../../../../lib/libcygwin.a(libcmain.o): In function `main':
/usr/src/debug/cygwin-1.7.30-1/winsup/cygwin/lib/libcmain.c:39: undefined reference to `WinMain'
/usr/src/debug/cygwin-1.7.30-1/winsup/cygwin/lib/libcmain.c:39:(.text.startup+0x7e): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `WinMain'
collect2: error: ld returned 1 exit status

Any suggestions? These files are well tested and generated the code fine before but now i think there might be some cygwin settings or so ... m compiling on windows 8 on cygwin.

Fortuity answered 28/5, 2014 at 17:36 Comment(1)
"gcc errlib.c -o errlib.o" is very confusing (it would generate an executable file with extension .o). It should be "gcc -c errlib.c", to generate the object file errlib.o; or "gcc errlib.c -o errlib.exe", to generate the executable file.Gladstone
M
18

Use -c compile flag to only produce object file. Without -c it tries to link an executable and the linker (called automatically) fails.

Monstrance answered 28/5, 2014 at 17:44 Comment(5)
but now if I $ gcc addtcpc.c -o addtcpc -l sockwrap errlib gcc: error: errlib: No such file or directory however if i list ls addtcpc.c errlib.c sockwrap.c errlib.h sockwrap.h errlib.o sockwrap.oFortuity
@user3448716 Then I don't fully understand your problem. If you just need to produce object file from source file (without linking an executable) you need to use -c. If you need to compile several sources and automatically link the executable after that you don't need to use -c. Please describe your workflow and what you are trying to achieve.Monstrance
In fact I have one file say xyz.c but this file use some functions defined in other 2 files named abc.h abc.c and def.h and def.c. Its not working when I do gcc xyz.c -o xyz -l abc.h def.hFortuity
Try gcc xyz.c abc.c def.c. Note that you need to have main() defined in one of these .c files. -I is used to tell compiler wich directories to scan for include files, not to supply header file names. You should #include "abc.h" and #include "def.h" in your xyz.c and also compile abc.c and def.c. That's what gcc xyz.c abc.c def.c does.Monstrance
@Monstrance gcc xyz.c abc.c def.c produces executable name a.exe, maybe we should also use the -o flag to specify output executable name so that there is no confusion while execution.Roberson

© 2022 - 2024 — McMap. All rights reserved.