Search code examples
gccshared-librariesdynamic-linkingmissing-symbols

shared library links or fails to link with another shared library, depending of that one's namespec


In the project I'm building from source (Nix 2.3 FWIW), one shared library (libnixstore.so) is being linked with another shared library (libnixutil.so). The command line for this is:

x86_64-slackware-linux-g++ -std=c++17 -o /d/tmp/SBo/nix-2.3/src/libstore/libnixstore.so -shared -L/usr/lib64 -Wl,--no-copy-dt-needed-entries src/libstore/binary-cache-store.o src/libstore/build.o src/libstore/builtins/buildenv.o src/libstore/builtins/fetchurl.o src/libstore/crypto.o src/libstore/derivations.o src/libstore/download.o src/libstore/export-import.o src/libstore/gc.o src/libstore/globals.o src/libstore/http-binary-cache-store.o src/libstore/legacy-ssh-store.o src/libstore/local-binary-cache-store.o src/libstore/local-fs-store.o src/libstore/local-store.o src/libstore/machines.o src/libstore/misc.o src/libstore/nar-accessor.o src/libstore/nar-info-disk-cache.o src/libstore/nar-info.o src/libstore/optimise-store.o src/libstore/parsed-derivations.o src/libstore/pathlocks.o src/libstore/profiles.o src/libstore/references.o src/libstore/remote-fs-accessor.o src/libstore/remote-store.o src/libstore/s3-binary-cache-store.o src/libstore/sqlite.o src/libstore/ssh-store.o src/libstore/ssh.o src/libstore/store-api.o -lsqlite3 -ldl -lbz2 -lcurl  -pthread -ldl -lseccomp -Wl,-z,defs -Wl,-soname=libnixstore.so    -Wl,-rpath,/d/tmp/SBo/nix-2.3/src/libutil -Lsrc/libutil -lnixutil

This command line is what 'configure' script produces. However, this fails, informing of not finding lots of symbols from 'libnixutil.so'. The unresolved symbols actually ARE there, with correct mangling (I checked, with 'nm' and 'readelf'). Now, if I just replace '-lnixutil' with 'src/libutil/libnixutil.so', linking completes okay. What's the difference, and is it documented? I see nothing appropriate in 'man ld', and (seemingly) nothing directly related in the net search.


Solution

  • Now, if I just replace '-lnixutil' with 'src/libutil/libnixutil.so', linking completes okay. What's the difference

    The difference is that the -lnuxutil searches for the library in various directories, while src/libutil/libnixutil.so doesn't.

    Since using -lnuxutil fails to link while it does find the library, it is safe to assume that it finds a version of this library somewhere other than in src/libutil, and the version it finds is probably wrong (i.e. older version).

    To see where the linker finds libnixutil, use the -Wl,-t flag (it will show all input files as they are being opened by the linker).