我在cygwin下使用
gcc编写一些代码,在我的代码中调用来自iphlpapi.h的GetAdaptersAddresses我已经将_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 ……