0

How can I force the gcc linker to link against a given version (soname) of a shared library on the system?

I need this to enforce that the version of openssl that is #include'ed matches the version that is linked, on any system, even if multiple versions of openssl are installed. To find the ABI version, my configure script first compiles and runs a program that extracts the SONAME from the headers:

#include <openssl/opensslv.h>
#include <stdio.h>

int main () {
  printf(SHLIB_VERSION_NUMBER);
  return 0;
}

The SHLIB_VERSION_NUMBER contains the so version string, e.g. 0.9.8 or 1.0.2k or 1.1.0. But how do I tell gcc to link against this version of libssl or libcrypto rather than just any -lssl?

I tried "in situ" linking, so that instead of linking with gcc main.c -lcrypto we use:

gcc main.c libcrypto.so.1.1.0

However it seems the linker libcrypto.so.1.1.0 cannot be found:

gcc: error: libcrypto.so.1.1.0: No such file or directory

I guess the system only searches in the standard locations when using the -l flag. Is there a better way to make my software link against libcrypto.so.1.1.0?

Jeroen Ooms
  • 31,998
  • 35
  • 134
  • 207
  • I think that what you wrote asks gcc to try to COMPILE libcrypto.so.1.1.0, rather than link it. There is a way of compiling the files in stages, then linking afterwards. – Thomas Hedden Feb 25 '17 at 16:08
  • 2
    @ThomasHedden: your assumption is wrong. If you passed an absolute path to a shared object there it would link to it perfectly fine. – datenwolf Feb 25 '17 at 16:40
  • When linking like this: `gcc main.c libcrypto.so.1.1.0`, then `libcrypto.so.1.1.0` needs to be on the path and on the link/loader path. You can perform `gcc main.c /usr/local/lib64/libcrypto.so.1.1.0 -o my_prog` to help with the compile problem, but I am not sure what will happen at runtime. At runtime, you may need tricks like `LD_LIBRARY=/usr/local/lib64/libcrypto.so.1.1.0 ./my_prog`. I also know using the staic archive works as expected: `gcc main.c /usr/local/lib64/libcrypto.a -o my_prog`. I use it frequently to avoid all the runtime link problems. – jww Feb 25 '17 at 18:57

1 Answers1

1

To select the correct version of the openssl shared libraries use:

gcc main.c -l:libssl.so.1.0.0 -l:libcrypto.so.1.0.0

The key to answering this question is "how do I control ld so that is links the correct version of a shared library?".

The correct way to pass a linker flag (a command line parameter to ld) using the gnu compilers (gcc, g++, gdc, etc...) is to use the normal ld parameter prefixed with "-l". For example -lssl or -L/usr/local/lib.

Edit: As per How to specify the library version to use at link time? you can read the manual for ld with:man ld.

Community
  • 1
  • 1
rmc
  • 1,018
  • 1
  • 9
  • 8