我在gcc
下使用cygwin
编写了一些代码,这些代码从我的代码中的GetAdaptersAddresses
调用iphlpapi.h
。我已将_WIN32_WINNT
设置在所需的0x0501
上方,并在链接器行中添加了-liphlpapi
,但链接器仍然失败,并显示以下消息:
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
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>
我知道他们在那里该死: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
有人看到我错过了吗?编辑:答案
# 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
最佳答案
如this question中所述,gcc参数的顺序很重要。您需要将库包含移动到依赖于它的对象之后。
所以gcc build/obj/*.o -liphlpapi ...
关于c++ - 未定义对 `_GetAdaptersAddresses@20'的引用-但我包括了-liphlpapi,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10972794/