本文介绍了为什么在使用 -rpath 时需要 -L?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在使用-rpath时必须要给出-L标志.例如:

I find that the -L flag must be given when using -rpath. For instance:

gcc -o test test.o -L. -lmylib -Wl,-rpath=.

为什么需要 -L 标志?编译时,除了来自 h 文件的信息之外,还需要什么信息?

Why is the -L flag needed? What information more than the information from the h-files are needed at compile time?

如果我删除 -L.我收到以下消息:

If I remove -L. I get the following message:

gcc -o test test.o -lmylib -Wl,-rpath=.
/usr/bin/ld: cannot find -lmyLib

不过,删除这两个标志是完全可以的.像这样:

It's perfectly ok to remove both flags, though. Like this:

gcc -o test test.o -lmylib

前提是libmyLib可以在/usr/lib中找到,即.为什么现在不需要-L?

Provided that libmyLib can be found in /usr/lib, that is. Why isn't -L needed now?

这是 https://stackoverflow.com/a/8482308/1091780.

推荐答案

即使是动态库也需要一定程度的静态链接;链接器需要知道动态库应该提供哪些符号.主要区别在于动态库在运行时提供定义,而完全静态库在链接时提供定义.

Even dynamic libraries required a degree of static linkage; the linker needs to know what symbols should be supplied by the dynamic library. The key difference is that the dynamic library provides the definition at runtime, whilst with fully static library provides the definition at link time.

因此,需要 -L 来指定要链接的文件的位置,就像 -l 指定特定的库一样.. 表示当前目录.

For this reason, -L is needed to specify where the file to link against is, just as -l specifies the specific library. The . indicates the current directory.

-rpath 在运行时发挥作用,当应用程序尝试加载动态库时.当尝试加载动态库时,它会通知程序要搜索的其他位置.

-rpath comes into play at runtime, when the application tries to load the dynamic library. It informs the program of an additional location to search in when trying to load a dynamic library.

不需要指定 -L/usr/lib 的原因是因为默认情况下链接器正在寻找那里(因为这是放置库的一个非常常见的地方).

The reason -L/usr/lib doesn't need to be specified is because the linker is looking there by default (as this is a very common place to put libraries).

这篇关于为什么在使用 -rpath 时需要 -L?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 19:46