Search code examples
linuxgcclinkershared-libraries

Why GCC doesn't recognize shared library in a file having numerical suffix after so


I noticed that GCC cannot find shared library if library file ends with numeric suffix instead of just .so. Shared library file name could have 2 number suffix for major and minor version.

origin library file location:

/nix/store/85j54v9a4jg98g6281fdxdvig90m1ywi-msodbcsql17-17.7.1.1-1/lib/libmsodbcsql-17.7.so.1.1
mkdir /tmp/mslib
cp /nix/store/85j54v9a4jg98g6281fdxdvig90m1ywi-msodbcsql17-17.7.1.1-1/lib/libmsodbcsql-17.7.so.1.1 /tmp/mslib
gcc -L/tmp/mslib -lmsodbcsql-17.7
/nix/store/a4mmjm3bblxwp8h53bcfx3dly80ib0ba-binutils-2.35.1/bin/ld: cannot find -lmsodbcsql-17.7
collect2: error: ld returned 1 exit status

same for library without numeric suffix

cp /nix/store/85j54v9a4jg98g6281fdxdvig90m1ywi-msodbcsql17-17.7.1.1-1/lib/libmsodbcsql-17.7.so.1.1 /tmp/mslib/libmsodbcsql-17.7.so

gcc -L/tmp/mslib -lmsodbcsql-17.7
/nix/store/a4mmjm3bblxwp8h53bcfx3dly80ib0ba-binutils-2.35.1/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

this time linker was able to find the library!

gcc --version
gcc (Debian 8.3.0-6) 8.3.0


Solution

  • This is just a convention they follow. Instead of making a copy of the library, I recommend using a symlink, like most distros do in their /lib or /usr/lib directories.

    ln -s /nix/store/85j54v9a4jg98g6281fdxdvig90m1ywi-msodbcsql17-17.7.1.1-1/lib/libmsodbcsql-17.7.so.1.1 /tmp/mslib/libmsodbcsql-17.7.so