LD_LIBRARY_PATH 与 LIBRARY_PATH [英] LD_LIBRARY_PATH vs LIBRARY_PATH

查看:44
本文介绍了LD_LIBRARY_PATH 与 LIBRARY_PATH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

<块引用>

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

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

<块引用>

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

然后我尝试设置 LIBRARY_PATH,并且奏效了.

根据 GCC 手册:

<块引用>

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

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

但是……

  • 自从我链接到 gcc 为什么 ld 是被调用,作为错误信息建议?
  • 这是什么意思有两个变量服务相同目的?有没有其他区别?

解决方案

LIBRARY_PATH 由 gcc 在编译前用于搜索包含需要链接到程序的静态和共享库的目录.

LD_LIBRARY_PATH 被你的程序用来在成功编译和链接后搜索包含共享库的目录.

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

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?

解决方案

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.

这篇关于LD_LIBRARY_PATH 与 LIBRARY_PATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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