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

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

问题描述

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



问题:-L选项的顺序顺序是否也很重要它为-l wrt链接器吗?



此链接: http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html 并没有多说关于-L的序列。



编辑
另外,


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


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

  gcc -lm hello.c -Lx 

code>

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

解决方案

是的, -L 选项的顺序很重要 - 只要l ike -l <​​/ code>和 -I 选项。



man ld


-Lsearchdir

- library-path = searchdir


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


GCC文件,更具体地说链接选项对您有用



编辑

抱歉,确实我错过了检查您提供的链接。 man ld只能写在控制台中。


Edit2

我做了一个简单的测试,把 -l <​​/ code >在 -L 选项之前,并且与 -L 之前的 -l



因此,回答第二个问题,这是

  gcc -lm hello.c -Lx 

等于这个

  gcc -Lx -lm hello.c 


$ b $在这两个测试中,libm都在x /目录中首先被搜索。



请注意,将 -l <​​lib>> 之前的源文件是一个不好的做法,这可能会导致链接时未定义的引用。这是正确的方法

  gcc hello.c -Lx -lm 


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.

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

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

EDIT Also,

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

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

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

解决方案

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

From 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 and more specifically Linking Options will be useful for you

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

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

is equal to this

gcc -Lx -lm hello.c

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

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