How to link a specific version of a shared library in makefile without using LD_LIBRARY_PATH?

25,347

Solution 1

I figured out that I can include the absolute path of the specific library that I want to link to and it worked fine for me:

LD = ${RPATH} -lsocket -lnsl -lpthread ${OPENSSLDIR}/lib/libssl.so.1.0.0 \
         ${OPENSSLDIR}/lib/libcrypto.so.1.0.0

If you are using g++, Piotr Lesnicki pointed out that -l:libssl.so.1.0.0 also works. See more at the original post.

Solution 2

Do you have any links to the SSL lib? If not, can you create a link to the the desired SSL lib like

ln -s libssl.so.1.0.0 libssl.so 

in the ssl directory and try it

Share:
25,347
Z.Zen
Author by

Z.Zen

Updated on July 27, 2022

Comments

  • Z.Zen
    Z.Zen almost 2 years

    I know that LD_LIBRARY_PATH is evil and it's a good habit to avoid using it. I have a program called server.c on a remote Solaris 9 server that holds two versions of openssl library (0.9.8 and 1.0.0) and I'm using gcc 3.4.6. My program need to link to 1.0.0a version. Because it's work environment, I don't have the right to modify anything in the openssl library directory. I figured out to compile my program with both -L and -R options without setting LD_LIBRARY_PATH and it worked fine. (I noticed it won't work without setting -R option) But the compiled program kept linking to /usr/local/ssl/lib/libssl.so.0.9.8 instead of /.../libssl.so.1.0.0. Is there a work-around for this?

    BTW, please correct me if I'm wrong: is it the -R option that actually "link" the shared libraries at runtime and -L option only "load" shared libraries at compile time?

    Any help will be much appreciated!

    Z.Zen

    //////////////////////////////////////////////

    Here is my Makefile:

    CC = gcc
    OPENSSLDIR = /usr/local/ssl
    CFLAGS = -g -Wall -W -I${OPENSSLDIR}/include -O2 -D_REENTRANT -D__EXTENSIONS__ 
    
    RPATH = -R${OPENSSLDIR}/lib
    LD = ${RPATH} -L${OPENSSLDIR}/lib -lssl -lcrypto -lsocket -lnsl -lpthread
    
    OBJS = common.o
    
    PROGS = server
    
    all: ${PROGS}
    
    server: server.o ${OBJS}
            ${CC} server.o ${OBJS} -o server ${LD}
    
    
    clean:;
            ${RM} ${PROGS} *.ln *.BAK *.bak *.o
    
  • Z.Zen
    Z.Zen almost 14 years
    yeah, symlink is a good way to do it but sometimes I need to link to older version of the openssl library. I want something that I can change every time I compile my program. But thanks, it works!
  • jgmjgm
    jgmjgm over 8 years
    I find that for gcc -l: does not work, but it will not produce an error either and still tries to link to something.