链接时LD_LIBRARY_PATH和-L有什么区别? [英] What is the difference between LD_LIBRARY_PATH and -L at link time?

查看:145
本文介绍了链接时LD_LIBRARY_PATH和-L有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 link 时发现 LD_LIBRARY_PATH 存在问题(此问题与运行时间无关)。

b
$ b

当我运行make(这是一个使用g ++版本4.1.x的Linux系统)时,链接线看起来像这样:

  g ++ ao bo co -o myapp \ 
-L ​​/ long / path / to / libs / \
-L ​​/ another / long / path / \
-labc -ldef -lghi

-l <​​/ code> options引用共享库(如libabc.so),它们存在于 -L 选项指定的目录中。这些目录也出现在 LD_LIBRARY_PATH 中。如果我从> LD_LIBRARY_PATH 目录中删除这些目录,则可以使用该配置文件,并且可以运行该应用程序。 >,那么我得到一个错误行,如:

$ p $ / usr / bin / ld:找不到-labc

另一方面,如果我从 -L options,然后我得到很多警告,例如:

$ p $ / usr / bin / ld:warning:libabc。所以,需要/long/path/to/libs/libxyz.so,找不到
(尝试使用-rpath或-rpath-link)

以及更多错误,例如:

  / long / path /to/libs/libdef.so:对'Foo :: Bar< Baz> :: junk(Fred *)''


的未定义引用

有人可以解释 LD_LIBRARY_PATH -L 之间的区别吗?我想深入了解这个东西,所以引用非常感谢!



另外,我必须添加到链接行以避免使用 LD_LIBRARY_PATH



编辑: < -L ,编译器建议尝试使用-rpath或-rpath-link。我以前从未见过makefile中的这些选项。你有吗?不知道这是否会帮助 LD_LIBRARY_PATH 问题。

LD_LIBRARY_PATH 的设置优先级最高,因此设置时,首先搜索由 LD_LIBRARY_PATH 提及的目录集甚至在目录的标准集合
之前。因此,在你的情况下,设置 LD_LIBRARY_PATH 会影响到查找

的库,这些库用 -l <​​/ code >选项。如果没有 LD_LIBRARY_PATH ,某些依赖关系

可能已从标准目录集中解析出来。


尽管设置 LD_LIBRARY_PATH 有助于调试并尝试更新版本的

a库在通用开发环境设置和部署中的使用被认为是不好的。


另请参阅来自Linux文档的这份HOWTO 了解共享库的更多详细信息


I'm having problems with LD_LIBRARY_PATH at link time (this question has nothing to do with run time).

The link line looks like this when I run make (this is a Linux system using g++ version 4.1.x):

g++ a.o b.o c.o -o myapp \
 -L/long/path/to/libs/ \
 -L/another/long/path/ \
 -labc -ldef -lghi

The -l options reference shared libraries (e.g., libabc.so) which exist in the directories specified by the -L options. Those directories also appear in LD_LIBRARY_PATH. With that configuration, the link is successful, and I can run the application.

If I remove the directories from LD_LIBRARY_PATH, then I get a single error line such as:

/usr/bin/ld: cannot find -labc

On the other hand, if I remove the directories from the list of -L options, then I get many warnings such as:

/usr/bin/ld: warning: libabc.so, needed by /long/path/to/libs/libxyz.so,
    not found (try using -rpath or -rpath-link)

and then many more errors, such as:

/long/path/to/libs/libdef.so: undefined reference to `Foo::Bar<Baz>::junk(Fred*)'

Can someone explain the difference between LD_LIBRARY_PATH and -L? I would like to understand this stuff in depth, so references are greatly appreciated!

Also, what do I have to add to the link line to avoid using LD_LIBRARY_PATH?

EDIT: When directories were missing from -L, the compiler suggested to "try using -rpath or -rpath-link". I don't think I've ever seen those options in a makefile before. Have you? Not sure if that would help the LD_LIBRARY_PATH problem though.

解决方案

The settings of LD_LIBRARY_PATH has the highest precedence, so when it is set, the set of directories mentioned by LD_LIBRARY_PATH are searched first even before the standard set
of directories. So in your case setting of LD_LIBRARY_PATH is influencing the lookup of
the libraries mentioned with -l option. Without LD_LIBRARY_PATH some of the dependencies
might have been resolved from the standard set of directories.

Though setting of LD_LIBRARY_PATH help with debugging and to try out a newer version of
a library its usage in the general development environment setup and deployment is considered bad.

Also refer this HOWTO from Linux Documentation for more details on Shared Libraries

这篇关于链接时LD_LIBRARY_PATH和-L有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆