我正在构建一个简单的 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?
发布评论
评论(4)
LIBRARY_PATH
由gcc 在编译之前使用,用于搜索包含需要链接到程序的静态库和共享库的目录。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.LD_LIBRARY_PATH
在程序启动时搜索,LIBRARY_PATH
在链接时搜索。来自评论的警告:
ld链接库时code>(而不是
gcc
或g++
),不会读取LIBRARY_PATH
或LD_LIBRARY_PATH
环境变量。gcc
或g++
链接库时,会读取LIBRARY_PATH
环境变量(请参阅 文档“gcc
在搜索普通库时使用这些目录”)。LD_LIBRARY_PATH
is searched when the program starts,LIBRARY_PATH
is searched at link time.caveat from comments:
ld
(instead ofgcc
org++
), theLIBRARY_PATH
orLD_LIBRARY_PATH
environment variables are not read.gcc
org++
, theLIBRARY_PATH
environment variable is read (see documentation "gcc
uses these directories when searching for ordinary libraries").当 gcc 处于链接模式时,它会在内部调用 ld。
gcc calls ld internally when it is in linking mode.
LIBRARY_PATH
由链接器 (ld) 使用LD_LIBRARY_PATH
由加载器 (ld.so) 使用LIBRARY_PATH
is used by linker (ld)LD_LIBRARY_PATH
is used by loader (ld.so)