paths in linux

There are many “path” in linux. Such as executable search PATH, compiler include path, linker path, dynamic library search path LD_LIBRARY_PATH.

PATH

PATH is an environment variable. It defines the executable search path. When you run executable file without path(absolute or relative path), it will search the executable file from directories that defined by PATH.

At initial, the PATH is defined in /etc/environment.

1
2
3
$ cat /etc/environment 

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

We can append the PATH in the terminal:

1
export $PATH=$PATH:/path/append

We can append the PATH in .bashrc(if terminal is bash) or .zshrc(if terminal is zsh), so the appened PATH is avaiable whenever we open a new terminal.

Include Path

For gcc, it will look in several different places for headers[1]. It will look for headers requested with #include in default directories. We can check the directories by using gcc --verbose

1
2
3
4
5
6
7
8
9
10
11
$ cpp --verbose

#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-linux-gnu/7/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed
/usr/x86_64-linux-gnu/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.

We can specify the include path when we compile the source code by adding -I/path/to/be/added.

Linker Search Path

In order to do the symbol resolution, linker will search for the unresolved symbols in linker search path.

We can use gcc -print-search-dirs | sed '/^lib/b 1;d;:1;s,/[^/.][^/]*/\.\./,/,;t 1;s,:[^=]*=,:;,;s,;,; ,g' | tr \; \\012
to print default linker search path:

1
2
3
4
$ gcc -print-search-dirs | sed '/^lib/b 1;d;:1;s,/[^/.][^/]*/\.\./,/,;t 1;s,:[^=]*=,:;,;s,;,;  ,g' | tr \; \\012

libraries:
/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/x86_64-linux-gnu/lib/x86_64-linux-gnu/7/:/usr/x86_64-linux-gnu/lib/x86_64-linux-gnu/:/usr/x86_64-linux-gnu/lib/:/usr/lib/x86_64-linux-gnu/7/:/usr/lib/x86_64-linux-gnu/:/usr/lib/:/lib/x86_64-linux-gnu/7/:/lib/x86_64-linux-gnu/:/lib/:/usr/lib/x86_64-linux-gnu/7/:/usr/lib/x86_64-linux-gnu/:/usr/lib/:/usr/x86_64-linux-gnu/lib/:/usr/lib/:/lib/:/usr/lib/

We can specify the linker search path to gcc by adding -L/path/to/be/added.

We can use -B/path/to/directory of gcc to specify the gcc search path. It will insert the path in the head of the searh path list.
For example, we add -B/usr/test/ path to gcc:

1
2
3
4
$ gcc -B/usr/test/ -print-search-dirs | sed '/^lib/b 1;d;:1;s,/[^/.][^/]*/\.\./,/,;t 1;s,:[^=]*=,:;,;s,;,;  ,g' | tr \; \\012

libraries:
/usr/test/x86_64-linux-gnu/7/:/usr/test/x86_64-linux-gnu/:/usr/test/:/usr/lib/gcc/x86_64-linux-gnu/7/:/usr/x86_64-linux-gnu/lib/x86_64-linux-gnu/7/:/usr/x86_64-linux-gnu/lib/x86_64-linux-gnu/:/usr/x86_64-linux-gnu/lib/:/usr/lib/x86_64-linux-gnu/7/:/usr/lib/x86_64-linux-gnu/:/usr/lib/:/lib/x86_64-linux-gnu/7/:/lib/x86_64-linux-gnu/:/lib/:/usr/lib/x86_64-linux-gnu/7/:/usr/lib/x86_64-linux-gnu/:/usr/lib/:/usr/x86_64-linux-gnu/lib/:/usr/lib/:/lib/:/usr/lib/

LD_LIBRARY_PATH

LD_LIBRARY_PATH is an environment variable you set to give the run-time shared library loader (ld.so) an extra set of directories to look for when searching for shared libraries[2].

Reference

  1. Gcc Search Path: https://gcc.gnu.org/onlinedocs/gcc-4.8.0/cpp/Search-Path.html
  2. Why LD_LIBRARY_PATH is bad: http://xahlee.info/UnixResource_dir/_/ldpath.html