Linking is the final step of creating an executable by combining all the required functions Functions are usually provided from the compiled source code and from external libraries. Typically, dynamic linking is employed, i.e. external libraries are not included into the executable but will be used only when the program runs. Libraries that are dynamically linked are also called shared libraries. Once share libraries are installed, they can be loaded by every program on the computer. This reduced the size of the executables but can cause problems when a program needs a library (in a certain version) to run.Linking a shared library is done via the -l option, which is followed by the name of the library without lib but with extension .so (shared object): -lfftw is looking for a file called libfftw.so. The search order is from left to right, i.e. the linker looks at the leftmost file and its requirements and checks every file to the right if it provides the references. stackexchange If some libraries (references) are missing when
LIBRARY_PATH is used by gcc before compilation to search for directories containing libraries that need to be linked to your program.
LD_LIBRARY_PATH is used by your program to search for directories containing the libraries after it has been successfully compiled and linked.
EDIT: As pointed below, your libraries can be static or shared. If it is static then the code is copied over into your program and you don't need to search for the library after your program is compiled and linked. If your library is shared then it needs to be dynamically linked to your program and that's when LD_LIBRARY_PATH comes into play.http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html http://linuxmafia.com/faq/Admin/ld-lib-path.html
-Wl,--as-needed-- MartinDiehl - 15 Mar 2016