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.

Paul
  • 13,042
  • 3
  • 41
  • 59
Fawad Nasim
  • 343
  • 2
  • 4
  • 10
  • 2
    "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. – Zanna Jan 31 '19 at 18:23

1 Answers1

17

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

Paul
  • 13,042
  • 3
  • 41
  • 59
  • 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.o – Fawad Nasim May 28 '14 at 18:21
  • @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. – Paul May 29 '14 at 09:43
  • 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.h – Fawad Nasim May 29 '14 at 12:21
  • 2
    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. – Paul May 29 '14 at 12:34
  • @Paul ```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. – Aritro Shome Jul 20 '21 at 14:39