Mingw 未定义引用 libws2_32 ('getnameinfo', 'getaddrinfo', ...)

Mingw undefined reference to libws2_32 ('getnameinfo', 'getaddrinfo', ...)

我正在尝试构建包含 libcurl+openssl+zlib 的应用程序(全部从源构建)。它给了我以下链接错误:

g++ -std=c++0x -DWINVER=0x0501 -DCURL_STATICLIB "-ID:\PROJEKTY\PC\libcurl_openssl_zlib\src\lib" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\main.o" "..\src\main.cpp" 
g++ "-LD:\PROJEKTY\PC\libcurl_openssl_zlib\src\lib\curl" "-LD:\PROJEKTY\PC\libcurl_openssl_zlib\src\lib\openssl" "-LD:\PROJEKTY\PC\libcurl_openssl_zlib\src\lib\zlib" -v -static-libgcc -static-libstdc++ -o libcurl_openssl_zlib.exe "src\main.o" -lcurl -lcurldll -lssl -lcrypto -lz -lwldap32 -lws2_32 
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/5.3.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-5.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --prefix=/mingw --disable-win32-registry --target=mingw32 --with-arch=i586 --enable-languages=c,c++,objc,obj-c++,fortran,ada --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libintl-prefix=/mingw --enable-libstdcxx-debug --with-tune=generic --enable-libgomp --disable-libvtv --enable-nls : (reconfigured) ../src/gcc-5.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --prefix=/mingw --disable-win32-registry --target=mingw32 --with-arch=i586 --enable-languages=c,c++,objc,obj-c++,fortran,ada --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --with-tune=generic --enable-libgomp --disable-libvtv --enable-nls
Thread model: win32
gcc version 5.3.0 (GCC) 
COMPILER_PATH=c:/mingw/bin/../libexec/gcc/mingw32/5.3.0/;c:/mingw/bin/../libexec/gcc/;c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/../lib/gcc/mingw32/5.3.0/;c:/mingw/bin/../lib/gcc/;c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/lib/;c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../
COLLECT_GCC_OPTIONS='-LD:\PROJEKTY\PC\libcurl_openssl_zlib\src\lib\curl' '-LD:\PROJEKTY\PC\libcurl_openssl_zlib\src\lib\openssl' '-LD:\PROJEKTY\PC\libcurl_openssl_zlib\src\lib\zlib' '-v' '-static-libgcc' '-o' 'libcurl_openssl_zlib.exe' '-mtune=generic' '-march=i586'
 c:/mingw/bin/../libexec/gcc/mingw32/5.3.0/collect2.exe -plugin c:/mingw/bin/../libexec/gcc/mingw32/5.3.0/liblto_plugin-0.dll -plugin-opt=c:/mingw/bin/../libexec/gcc/mingw32/5.3.0/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\Jacek\AppData\Local\Temp\cchJKgec.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmsvcrt -Bdynamic -o libcurl_openssl_zlib.exe c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../crt2.o c:/mingw/bin/../lib/gcc/mingw32/5.3.0/crtbegin.o -LD:\PROJEKTY\PC\libcurl_openssl_zlib\src\lib\curl -LD:\PROJEKTY\PC\libcurl_openssl_zlib\src\lib\openssl -LD:\PROJEKTY\PC\libcurl_openssl_zlib\src\lib\zlib -Lc:/mingw/bin/../lib/gcc/mingw32/5.3.0 -Lc:/mingw/bin/../lib/gcc -Lc:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../../mingw32/lib -Lc:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../.. src\main.o -lcurl -lcurldll -lssl -lcrypto -lz -lwldap32 -lws2_32 -Bstatic -lstdc++ -Bdynamic -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc -lgcc_eh -lmoldname -lmingwex -lmsvcrt c:/mingw/bin/../lib/gcc/mingw32/5.3.0/crtend.o
D:\PROJEKTY\PC\libcurl_openssl_zlib\src\lib\openssl/libcrypto.a(b_addr.o):b_addr.c:(.text+0xaa): undefined reference to `getnameinfo'
D:\PROJEKTY\PC\libcurl_openssl_zlib\src\lib\openssl/libcrypto.a(b_addr.o):b_addr.c:(.text+0xb48): undefined reference to `getaddrinfo'
D:\PROJEKTY\PC\libcurl_openssl_zlib\src\lib\openssl/libcrypto.a(b_addr.o):b_addr.c:(.text+0xca0): undefined reference to `getaddrinfo'
D:\PROJEKTY\PC\libcurl_openssl_zlib\src\lib\openssl/libcrypto.a(b_addr.o):b_addr.c:(.text+0x819): undefined reference to `freeaddrinfo'
D:\PROJEKTY\PC\libcurl_openssl_zlib\src\lib\openssl/libcrypto.a(init.o):init.c:(.text+0x212): undefined reference to `GetModuleHandleEx'
D:\PROJEKTY\PC\libcurl_openssl_zlib\src\lib\openssl/libcrypto.a(init.o):init.c:(.text+0x984): undefined reference to `GetModuleHandleEx'
collect2.exe: error: ld returned 1 exit status

如上所示,-lws2_32 放在 -lcrypto 之后,这意味着它不应该是链接顺序的问题。 "Just in case",我也按照类似线程中的建议做了 -DWINVER=0x0501,但没有结果,尽管我看不出这将如何影响链接期间的错误(相反,它会抛出错误或警告关于编译期间的隐式声明)。

我向链接器添加了详细标志以查看库搜索路径,它们是正确的。前往 libws2_32.a 所在的位置并在其上 运行 nm returns 以下内容:

MSYS /c/MinGW/lib
$ nm.exe -g libws2_32.a | grep getnameinfo
00000000 I __imp__getnameinfo@28
00000000 T _getnameinfo@28

看起来有问题的函数就在那里。

我错过了什么?

您的 libws2_32.a 不是真正的静态库,而是 导入库 。这只是 windows 上的一个存根,需要在运行时动态链接 dll。

要使用它,您必须绑定前缀为 __imp__ 的符号(而不仅仅是 _)。这就是 __declspec(dllimport) 为您所做的。所以,不知何故,在编译你的 openssl 时,ws2_32 原型中缺少它(它现在应该链接到一个普通的静态库)。