gcc - GNU 链接器中 -l 和 -L 选项的顺序重要吗?

标签 gcc linker ld binutils

-l 选项告诉链接器在标准目录中搜索库。 并且通过-L,我们可以指定自己的库目录进行搜索。

问题:顺序对于 -L 选项是否也很重要,就像对于链接器的 -l 一样?

此链接:http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html没有过多说明 -L 的顺序。

编辑 另外,

Directories specified on the command line are searched before the default directories

来自手册页(正如 Dmitry 所指出的),这是否意味着即使我指定如下顺序:

gcc -lm hello.c -Lx

仍然优先考虑使用-L指定的目录吗?

最佳答案

是的,顺序是-L选项很重要 - 就像 -l-I选项。

来自 man ld

-Lsearchdir
--library-path=searchdir

Add path searchdir to the list of paths that ld will search for archive libraries and ld control scripts. You may use this option any number of times. The directories are searched in the order in which they are specified on the command line. Directories specified on the command line are searched before the default directories. All -L options apply to all -l options, regardless of the order in which the options appear.

<删除> GCC documentations更具体地说Linking Options对你有用

编辑
抱歉,我确实错过了检查您提供的链接。 “man ld”可以在控制台中写入。

编辑2
我做了一个简单的测试把-l之前-L选项,与 -L 相比没有任何区别之前-l

所以回答你的第二个问题,这个

gcc -lm hello.c -Lx

等于这个

gcc -Lx -lm hello.c

在两个测试中,首先在目录 x/中搜索 libm。

请注意,输入 -l<lib>在源文件之前是一个不好的做法,这可能会导致链接时出现 undefined reference 。这才是正确的做法

gcc hello.c -Lx -lm 

关于gcc - GNU 链接器中 -l 和 -L 选项的顺序重要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5817269/

相关文章:

c++ - g++中的目标文件是否有等效的-I(大写字母I)?

c - 在 Windows 上使用 MinGW 的 GMP

c++ - 即使启用了 C++11,静态成员初始化也不适用于 GCC

c - “# 1 "/usr/include/stdio.h"1 3 4” 在 "gcc -E"输出中意味着什么?

gcc -/usr/bin/ld : cannot find -lauparse

c++ - 运行程序时找不到共享对象库,但在编译过程中已链接

c++ - 如何自动链接到共享库的依赖项

gcc - LD_LIBRARY_PATH 与 LIBRARY_PATH

c++ - 为什么 GCC 优化掉这个增量?

c - gcc没有这样的文件或目录错误