GNU 链接器中 -l 和 -L 选项的顺序是否重要? [英] Does the order of -l and -L options in the GNU linker matter?

查看:19
本文介绍了GNU 链接器中 -l 和 -L 选项的顺序是否重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The -l option tells the linker to search the libraries in the standard dirs. And with -L, we can specify our own library directories for searching.

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

Question: Does the sequence of order matters for the -L option too, like it does for the -l w.r.t the linker?

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

This link: http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html doesn't say much about the sequence of -L.

编辑还有,

命令中指定的目录在默认值之前搜索行目录

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

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

is from the man page (as pointed by Dmitry), does this mean that even if I specify the order like:

gcc -lm hello.c -Lx

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

still the directory specified with -L will be given preference first?

推荐答案

是的,-L 选项的顺序很重要——就像 -l-我选项.

Yes, the order of -L options matters - just like -l and -I options.

来自 man ld

-Lsearchdir
--library-path=searchdir

将路径 searchdir 添加到 ld 将搜索归档库和 ld 控制脚本的路径列表中.您可以多次使用此选项.目录按照在命令行中指定的顺序进行搜索.在默认目录之前搜索命令行上指定的目录.所有 -L 选项都适用于所有 -l 选项,无论选项出现的顺序如何.

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 文档,更具体地说是 链接选项对你有用

GCC documentations and more specifically Linking Options will be useful for you

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

Edit
Sorry, indeed I missed to check the link you've given. "man ld" can just be written in the console.

编辑2
我做了一个简单的测试,将 -l 放在 -L 选项之前,它与 -L 之前的 -l 相比没有区别代码>

Edit2
I made a simple test putting -l before -L options and it shows no difference comparing to -L before -l

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

So answering your second question, this

gcc -lm hello.c -Lx

等于这个

gcc -Lx -lm hello.c

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

libm is searched first in directory x/ in both tests.

请注意,尽管将 -l<lib> 放在源文件之前是一种不好的做法,但这可能会导致链接时未定义的引用.这是正确的方法

Note though that putting -l<lib> before source files is a bad practice, that may lead to undefined references when linking. This is the correct way

gcc hello.c -Lx -lm 

这篇关于GNU 链接器中 -l 和 -L 选项的顺序是否重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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