Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying order in gcc and g++ include and lib paths

Tags:

c++

g++

linker

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.

like image 499
narengi Avatar asked Jan 03 '17 06:01

narengi


People also ask

Does the order of compiler flags matter?

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.

Does order matter in gcc?

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.

Where does gcc look for include files?

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.

Which option in gcc is used to specify the location of the .h files?

gcc -I adds include directory of header files.


1 Answers

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

like image 158
Mike Kinghan Avatar answered Sep 20 '22 16:09

Mike Kinghan