10

I'm using gcc under cygwin to write some code that calls GetAdaptersAddresses from iphlpapi.h in my code I've set _WIN32_WINNT above the required 0x0501 and on the linker line I've added -liphlpapi but the linker is still failing with the following message:

gcc -liphlpapi build/obj/*.o -Wall -Wextra -o build/bin/asdf.exe src/asdf.cpp
/tmp/ccdjLPVY.o:asdf.cpp:(.text+0x153): undefined reference to `_GetAdaptersAddresses@20'
collect2: ld returned 1 exit status

Some snippets from asdf.cpp:

#if _WIN32_WINNT < 0x0501
  #warning _WIN32_WINNT was set lower than 0x0501, fixing
  #undef _WIN32_WINNT
  #define _WIN32_WINNT 0x0501
#endif
#include <winsock2.h>
#include <iphlpapi.h>

I know they're there darn it:

strings /usr/i686-pc-mingw32/sys-root/mingw/lib/libiphlpapi.a  | sort | uniq | grep GetAdapters
__imp__GetAdaptersAddresses@20
__imp__GetAdaptersInfo@8
_GetAdaptersAddresses@20
_GetAdaptersInfo@8
GetAdaptersAddresses
GetAdaptersInfo

$strings /usr/lib/w32api/libiphlpapi.a  | sort | uniq | grep GetAdapters
__imp__GetAdaptersAddresses@20
__imp__GetAdaptersInfo@8
_GetAdaptersAddresses@20
_GetAdaptersInfo@8
GetAdaptersAddresses
GetAdaptersInfo

Is anyone seeing what I've missed?

Edit: Answer

# Change the order, put the linker options last:
# Before:
gcc -liphlpapi build/obj/*.o -Wall -Wextra -o build/bin/asdf.exe src/asdf.cpp
# After:
gcc            build/obj/*.o -Wall -Wextra -o build/bin/asdf.exe src/asdf.cpp -liphlpapi
Community
  • 1
  • 1
Huckle
  • 1,578
  • 2
  • 23
  • 38
  • [This question](http://stackoverflow.com/questions/10970356/undefined-reference-to-mysql-init) talks about how the order matters. Does that help? – chris Jun 10 '12 at 22:26
  • @chris That did the trick, please submit an answer with that link – Huckle Jun 10 '12 at 23:29

2 Answers2

9

As explained in this question, the order of gcc arguments matters. You need to move the library inclusion to after the objects that depend on it.

So gcc build/obj/*.o -liphlpapi ...

rogerdpack
  • 56,766
  • 33
  • 241
  • 361
chris
  • 58,138
  • 13
  • 137
  • 194
1

try adding #pragma comment(lib, "Iphlpapi.lib")

justcoder
  • 66
  • 4
  • This doesn't work on GCC. I remember trying it extensively when my settings kept resetting due to a technicality of it being on a flash drive. – chris Jun 10 '12 at 23:07
  • @chris because the `#pragma` is sets exclusively a compiler defined directives. – Hi-Angel Sep 08 '14 at 15:33
  • 1
    @Hi-Angel, Yes, it is. My point was that the OP obviously uses GCC and this answer only works on MSVC. – chris Sep 08 '14 at 17:50