c语言 - Mingw 未定义的对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:PROJEKTYPClibcurl_openssl_zlibsrclibcurl' '-LD:PROJEKTYPClibcurl_openssl_zlibsrclibopenssl' '-LD:PROJEKTYPClibcurl_openssl_zlibsrclibzlib' '-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:UsersJacekAppDataLocalTempcchJKgec.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:PROJEKTYPClibcurl_openssl_zlibsrclibcurl -LD:PROJEKTYPClibcurl_openssl_zlibsrclibopenssl -LD:PROJEKTYPClibcurl_openssl_zlibsrclibzlib -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/../../.. srcmain.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:PROJEKTYPClibcurl_openssl_zlibsrclibopenssl/libcrypto.a(b_addr.o):b_addr.c:(.text+0xaa): undefined reference to `getnameinfo'
D:PROJEKTYPClibcurl_openssl_zlibsrclibopenssl/libcrypto.a(b_addr.o):b_addr.c:(.text+0xb48): undefined reference to `getaddrinfo'
D:PROJEKTYPClibcurl_openssl_zlibsrclibopenssl/libcrypto.a(b_addr.o):b_addr.c:(.text+0xca0): undefined reference to `getaddrinfo'
D:PROJEKTYPClibcurl_openssl_zlibsrclibopenssl/libcrypto.a(b_addr.o):b_addr.c:(.text+0x819): undefined reference to `freeaddrinfo'
D:PROJEKTYPClibcurl_openssl_zlibsrclibopenssl/libcrypto.a(init.o):init.c:(.text+0x212): undefined reference to `GetModuleHandleEx'
D:PROJEKTYPClibcurl_openssl_zlibsrclibopenssl/libcrypto.a(init.o):init.c:(.text+0x984): undefined reference to `GetModuleHandleEx'
collect2.exe: error: ld returned 1 exit status

如上所示,-lws2_32放在-lcrypto之后,这意味着它不应该是链接顺序的问题。"以防万一",我也按照 simiar 线程中的建议-DWINVER=0x0501没有结果,即使我看不出这会如何影响链接期间的错误(相反,它会抛出错误或关于编译期间隐式声明的警告(。

我向链接器扔了详细的标志以查看库搜索路径,它们是正确的。转到 libws2_32.a 所在的位置并在其上运行nm将返回以下内容:

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

看起来有问题的功能就在那里。

我错过了什么?

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

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

最新更新