I have multiple versions of a library, all with the same name (boost libraries), each installed in its own directory. I know how to instruct the compiler and linker to search in certain directories for header files and libraries (-I
and -L
). I am also aware of how to pass the actual library file to the linker.
My question is how to specify precedence in the search path of the compiler and the linker, such that it searches folder A
before searching folder B
and takes A
's version of the library instead of B
's . I'm interested in order between all eligible directories, i.e. the default gcc
and g++
ones, and the ones specified after -I
and -L
.
My distro is Ubuntu 14.04
, and I use g++
4.8
up to 6
.
For the most part, the order you use doesn't matter. Order does matter when you use several options of the same kind; for example, if you specify -L more than once, the directories are searched in the order specified. Also, the placement of the -l option is significant.
The gcc program accepts options and file names as operands. Many options have multi-letter names; therefore multiple single-letter options may not be grouped: -dr is very different from -d -r. You can mix options and other arguments. For the most part, the order you use doesn't matter.
GCC looks for headers requested with #include " file " first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets. For example, if /usr/include/sys/stat. h contains #include "types.
gcc -I adds include directory of header files.
GCC will search your -I
directories in the left-to-right order in which they
appear in your commandline and it will search all your -I
directories before
the default #include
directories. Here is the documentation.
GCC invokes the system linker, ld
, to perform
linkage. Occurrences of GCC's -L
option, and its -l
option, are passed through
to the linker with their order preserved.
The linker will search your -L
directories in the left-to-right order in which
they appear in the commandline and it will search all your -L
directories
before the default linkage directories. All of the -L
options, in the order specified,
apply to all of the -l
options, regardless of how -L
and -l
options are intermixed
in the commandline. E.g.
-La -lfoo -Lb -lbar
is equivalent to any of:
-La -Lb -lfoo -lbar
-lfoo -La -Lb -lbar
-lfoo -lbar -La -Lb
Here is the documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With