LD_LIBRARY_PATH 与 LIBRARY_PATH

发布于 2024-10-03 11:43:53 字数 762 浏览 5 评论 0 原文

我正在构建一个简单的 C++ 程序,我想暂时用更新版本的系统提供的共享库替换它,以进行开发和测试。

我尝试设置 LD_LIBRARY_PATH 变量,但链接器 (ld) 失败,并显示:

/usr/bin/ld:找不到-lyaml-cpp

我希望它能工作,因为根据 ld 手册页:

链接器使用以下搜索 找到所需共享的路径 库:...对于本机链接器, 环境变量的内容 “LD_LIBRARY_PATH”...

然后我尝试设置LIBRARY_PATH,并且成功了。

根据海湾合作委员会手册:

价值 LIBRARY_PATH 是一个以冒号分隔的列表 目录,很像 PATH。什么时候 配置为本机编译器 GCC 尝试指定的目录 当搜索特殊链接器时 文件,如果找不到它们使用 GCC_EXEC_PREFIX。使用 GCC 链接 也使用这些目录时 搜索普通图书馆 -l 选项(但目录 先用 -L 指定)。

正如 (GCC) 手册所建议的那样,LIBRARY_PATH 可以工作,因为我与 GCC 链接。

但是..

  • 由于我与 gcc 链接,为什么 ld 是 被调用,作为错误消息 建议?
  • 有什么意义 有两个变量具有相同的作用 目的?还有其他的吗 差异?

I'm building a simple C++ program and I want to temporarily substitute a system supplied shared library with a more recent version of it, for development and testing.

I tried setting the LD_LIBRARY_PATH variable but the linker (ld) failed with:

/usr/bin/ld: cannot find -lyaml-cpp

I expected that to work because according to the ld man page:

The linker uses the following search
paths to locate required shared
libraries: ... For a native linker,
the contents of the environment variable
"LD_LIBRARY_PATH"...

I then tried setting the LIBRARY_PATH, and that worked.

According to the GCC manual:

The value of
LIBRARY_PATH is a colon-separated list
of directories, much like PATH. When
configured as a native compiler, GCC
tries the directories thus specified
when searching for special linker
files, if it can't find them using
GCC_EXEC_PREFIX. Linking using GCC
also uses these directories when
searching for ordinary libraries for
the -l option (but directories
specified with -L come first).

As the (GCC) manual suggests, LIBRARY_PATH works because I link with GCC.

But..

  • Since I link with gcc why ld is
    being called, as the error message
    suggests?
  • What's the point of
    having two variables serving the same
    purpose? Are there any other
    differences?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ゃ懵逼小萝莉 2024-10-10 11:43:53

LIBRARY_PATHgcc 在编译之前使用,用于搜索包含需要链接到程序的静态库和共享库的目录。

LD_LIBRARY_PATH 用于您的程序在成功编译和链接后搜索包含共享库的目录。

编辑:
如下所述,您的库可以是静态的或共享的。如果它是静态的,那么代码将被复制到您的程序中,并且在编译和链接程序后您不需要搜索库。如果您的库是共享的,那么它需要动态链接到您的程序,这就是 LD_LIBRARY_PATH 发挥作用的时候。

LIBRARY_PATH is used by gcc before compilation to search directories containing static and shared libraries that need to be linked to your program.

LD_LIBRARY_PATH is used by your program to search directories containing shared libraries after it has been successfully compiled and linked.

EDIT:
As pointed below, your libraries can be static or shared. If it is static then the code is copied over into your program and you don't need to search for the library after your program is compiled and linked. If your library is shared then it needs to be dynamically linked to your program and that's when LD_LIBRARY_PATH comes into play.

久随 2024-10-10 11:43:53

LD_LIBRARY_PATH 在程序启动时搜索,LIBRARY_PATH 在链接时搜索。

来自评论的警告:

LD_LIBRARY_PATH is searched when the program starts, LIBRARY_PATH is searched at link time.

caveat from comments:

烧了回忆取暖 2024-10-10 11:43:53

既然我与 gcc 链接,为什么 ld 被调用,如错误消息所示?

当 gcc 处于链接模式时,它会在内部调用 ld。

Since I link with gcc why ld is being called, as the error message suggests?

gcc calls ld internally when it is in linking mode.

笑梦风尘 2024-10-10 11:43:53

LIBRARY_PATH 由链接器 (ld) 使用

LD_LIBRARY_PATH 由加载器 (ld.so) 使用

LIBRARY_PATH is used by linker (ld)

LD_LIBRARY_PATH is used by loader (ld.so)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文