11

I have a simple socket program that I'm trying to compile using g++ running in mingw (both the latest versions) on a Win8 system. I'm getting the common linker errors

undefined reference to `__imp_socket'
undefined reference to `__imp_gethostbyname'

I've tried adding -lws2_32 with no luck; i.e. it still can't find the references. Can someone suggest something else I might be missing?

Here's the full output:

G:\source\kak>g++ -o ./test_client -lws2_32 test_client.C
C:\Users\kenkahn\AppData\Local\Temp\ccDZTr9b.o:test_client.C:(.text+0x4f): undefined reference to `__imp_inet_addr'
C:\Users\kenkahn\AppData\Local\Temp\ccDZTr9b.o:test_client.C:(.text+0x6b): undefined reference to `__imp_socket'
C:\Users\kenkahn\AppData\Local\Temp\ccDZTr9b.o:test_client.C:(.text+0x8b): undefined reference to `__imp_connect'
d:/program files/mingw/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.1/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\kenkahn\AppData\Local\Temp\ccDZTr9b.o: bad reloc address 0xc in section `.xdata'
collect2.exe: error: ld returned 1 exit status
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
kenkahn
  • 111
  • 1
  • 1
  • 6
  • Post the output of your `g++` command with `-v` added to the options. Also, keep in mind that "latest versions" isn't very helpful with gcc or MinGW since there are many different sources for these. For example, it looks like you're using gcc 4.8.1 - I have a MinGW install with gcc 4.8.2. – Michael Burr Mar 11 '14 at 00:41

1 Answers1

21

Try putting the -lws2_32 after the test_client.C parameter. The linker of gcc (ld) is touchy about the order of linkable things, this is probably why it doesn't find your imported functions at link time.

pasztorpisti
  • 3,760
  • 1
  • 16
  • 26
  • 2
    If this answer solved your problem, please **accept** it (green tick). – ollo Mar 11 '14 at 13:50
  • I wasted half an hour on this, probably would have battled for another hour without your answer. Thank you. – Leogout Jun 06 '23 at 20:15