如何在不使用 LD_LIBRARY_PATH 的情况下在 makefile 中链接共享库的特定版本?
我知道 LD_LIBRARY_PATH 是邪恶的,避免使用它是一个好习惯。 我在远程 Solaris 9 服务器上有一个名为 server.c
的程序,该服务器包含两个版本的 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
。有解决方法吗?
顺便说一句,如果我错了,请纠正我: -R
选项是否在运行时实际“链接”共享库,而 -L
选项仅“加载”共享编译时的库?
任何帮助将不胜感激!
Z.Zen
//////////////////////////////////////////////
这里是我的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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现我可以包含我想要链接的特定库的绝对路径,它对我来说效果很好:
如果您使用的是 g++,Piotr Lesnicki 指出
-l:libssl.so.1.0.0
也可以工作。请参阅原始帖子了解更多信息。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:
If you are using g++, Piotr Lesnicki pointed out that
-l:libssl.so.1.0.0
also works. See more at the original post.您有 SSL 库的链接吗?
创建指向所需 SSL 库的链接并尝试一下吗
如果没有,您可以在 ssl 目录中
Do you have any links to the SSL lib?
If not, can you create a link to the the desired SSL lib like
in the ssl directory and try it