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 | $ cat /etc/environment |
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 gcc --verbose
1 | $ cpp --verbose |
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 | $ gcc -print-search-dirs | sed '/^lib/b 1;d;:1;s,/[^/.][^/]*/\.\./,/,;t 1;s,:[^=]*=,:;,;s,;,; ,g' | tr \; \\012 |
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 | $ gcc -B/usr/test/ -print-search-dirs | sed '/^lib/b 1;d;:1;s,/[^/.][^/]*/\.\./,/,;t 1;s,:[^=]*=,:;,;s,;,; ,g' | tr \; \\012 |
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
- Gcc Search Path: https://gcc.gnu.org/onlinedocs/gcc-4.8.0/cpp/Search-Path.html
- Why LD_LIBRARY_PATH is bad: http://xahlee.info/UnixResource_dir/_/ldpath.html