21

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?

Ajay
  • 18,086
  • 12
  • 59
  • 105
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • `still the library specified with -L`... : -L does _not_ specify a library (it specifies search directories) – sehe Apr 28 '11 at 11:42
  • @sehe thanks, that was a typo (now I've edited), I haven't yet received an answer to my second question. – Aquarius_Girl Apr 28 '11 at 12:20
  • 1
    possible duplicate of [Why does the order of '-l' option in gcc matter?](http://stackoverflow.com/questions/11893996/why-does-the-order-of-l-option-in-gcc-matter) – pattivacek Mar 05 '14 at 16:15

1 Answers1

18

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 
Dmitry Yudakov
  • 15,364
  • 4
  • 49
  • 53
  • Thanks, that must be documented somewhere, I can't find it, if you have a link which confirms the thing, I'll be grateful. – Aquarius_Girl Apr 28 '11 at 10:42
  • Grateful to you :hattip: I did search Google before asking here, with the keywords 'man ld' and the second link I got was: http://linux.die.net/man/8/ld-linux, I prefer _die.net_ w.r.t man pages, but this time I mistook 'ld-linux' for 'ld'. Thanks for pointing out. – Aquarius_Girl Apr 28 '11 at 10:50
  • Did you not click the link I posted in my first post? Your second link is the same. And I do follow the GNU's official online docs. Thanks for bothering :) – Aquarius_Girl Apr 28 '11 at 10:52
  • @Dmitry please see the edit in my first post too and see if you can comment on it? – Aquarius_Girl Apr 28 '11 at 10:57
  • @Dmitry thanks very much, so it means that the used specified locations are searched first as compared to standard directories, irrespective of the placement order. – Aquarius_Girl Apr 29 '11 at 05:05
  • So -L indicates the library dir, and -l indicates the library to use. In this case, the file libm.a should be in the dir ./x ? – qed Feb 07 '14 at 17:04
  • @qed Yes, the library file (.a or .so) should be in directory x/ according to given examples. Note that libm.a is just a random library name in this example, don't get confused with system math library libm.so/.a – Dmitry Yudakov Feb 11 '14 at 15:22