LD_LIBRARY_PATH与LIBRARY_PATH [英] LD_LIBRARY_PATH vs LIBRARY_PATH

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

问题描述

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



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


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


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


链接器使用以下搜索
路径来查找所需的共享
库:...对于本机链接器,
环境变量$ b $的内容bLD_LIBRARY_PATH...

然后尝试设置LIBRARY_PATH,并且工作。



根据GCC手册:



LIBRARY_PATH的值是以冒号分隔的列表
目录,很像PATH。当
配置为本机编译器时,如果搜索特殊链接程序
文件(如果无法使用
GCC_EXEC_PREFIX找到它们),GCC
将尝试指定
的目录。当

搜索普通库时,使用GCC
的链接也会使用这些目录 - -l选项(但是指定使用-L指定的目录
)。


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



但是。 。


  • 由于我与gcc链接,为什么ld是
    被调用,因为错误消息
    表明?

  • 有两个变量用于同样的
    目的的
    有什么意义?是否还有其他
    的差异?

解决方案

LIBRARY_PATH在编译之前,gcc使用来搜索需要链接到程序的包含 static 库的目录。

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 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天全站免登陆