Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
605 views
in Technique[技术] by (71.8m points)

gcc - Does the order of -l and -L options in the GNU linker matter?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...