C语言 如何在不使用 LD_LIBRARY_PATH 的情况下在 makefile 中链接特定版本的共享库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3384897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:03:36  来源:igfitidea点击:

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

cgccopensslshared-libraries

提问by Z.Zen

I know that LD_LIBRARY_PATH is eviland it's a good habit to avoid using it. I have a program called server.con 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 -Land -Roptions without setting LD_LIBRARY_PATHand it worked fine. (I noticed it won't work without setting -Roption) But the compiled program kept linking to /usr/local/ssl/lib/libssl.so.0.9.8instead of /.../libssl.so.1.0.0. Is there a work-around for this?

我知道LD_LIBRARY_PATH 是邪恶的,避免使用它是一个好习惯。我有一个server.c在远程 Solaris 9 服务器上调用的程序,该服务器包含两个版本的 openssl 库(0.9.8 和 1.0.0),我使用的是 gcc 3.4.6。我的程序需要链接到 1.0.0a 版本。因为是工作环境,我无权修改openssl库目录下的任何东西。我想在没有设置的情况下使用-L-R选项编译我的程序,LD_LIBRARY_PATH它运行良好。(我注意到如果没有设置-R选项它就无法工作)但是编译的程序一直链接到/usr/local/ssl/lib/libssl.so.0.9.8而不是/.../libssl.so.1.0.0. 有解决方法吗?

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

顺便说一句,如果我错了,请纠正我:它是-R在运行时实际“链接”共享库的选项,而-L选项仅在编译时“加载”共享库吗?

Any help will be much appreciated!

任何帮助都感激不尽!

Z.Zen

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

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

Here is my Makefile:

这是我的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

回答by Z.Zen

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 Lesnickipointed out that -l:libssl.so.1.0.0also works. See more at the original post.

如果您使用g++Piotr Lesnicki指出这-l:libssl.so.1.0.0也有效。在原帖中查看更多信息

回答by Raghuram

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

你有任何指向 SSL 库的链接吗?如果没有,您能否创建一个指向所需 SSL 库的链接,例如

ln -s libssl.so.1.0.0 libssl.so 

in the ssl directory and try it

在ssl目录下试试