简体   繁体   中英

Undefined reference in an object file - how find which library contains it?

I have successfully compiled openssl library with Windows mingw... Then I'm linking it with my application, but it fails with unresolved symbol _imp__shutdown . The nm tells the object file really references the symbol.

How can one find out which library should be added to ld to resolve that dependency? Is there a standard procedure for such cases?

Linking:

> g++ -static -Wl,--allow-multiple-definition -mthreads -Wl,-subsystem,windows ... -lws2_32 -lshlwapi ... -lssl -lcrypto ...

C:/openssl-1.0.1g-mgw\libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x90): undefined reference to `_imp__shutdown@8'
C:/openssl-1.0.1g-mgw\libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x1a0): undefined reference to `_imp__shutdown@8'
c:/MinGW/bin/../lib/gcc/i686-w64-mingw32/4.8.2/../../../../i686-w64-mingw32/bin/ld.exe: C:/openssl-1.0.1g-mgw\libcrypto.a(bss_sock.o): bad reloc address 0x4 in section `.data'
collect2.exe: error: ld returned 1 exit status

The unresolved symbol:

> nm /c/openssl-1.0.1g-mgw/crypto/bio/bss_sock.o
            ...
            U __imp__closesocket@4
            U __imp__recv@16
            U __imp__send@16
            U __imp__shutdown@8
            U __imp__WSAGetLastError@0
            U __imp__WSASetLastError@4
            ...

GCC / Windows 7 x64:

> g++ --version
g++.exe (i686-posix-dwarf-rev3, Built by MinGW-W64 project) 4.8.2

Since you are doing static linking with gcc, you need to provide the libraries in reverse order of dependencies. If needed to resolve circular dependencies, you must provide the library names several times on the command line (eg if libfoo depends on libbar and libbar depends on libfoo, do -lfoo -lbar -lfoo)

In this case, since -lssl depends on these socket functions that are defined in the ws2_32 library, add -lws2_32 after -lssl

And to answer the actual question you pose, if you want to find which library provides a function, go to the documentation for that function ,eg the shutdown function documents that you need to link to Ws2_32.lib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM