Some notes on linker errors

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
  1. ) linking the executable
  2. ) running the executable
an error appears.

Unfortunately, even on the same system a successful linking operation does not necessary mean that running the executable will be successful as search paths differ. While linking, the order is the following
  1. )
While executing, the order is the following stackexchange
  1. ) LD_LIBRARY_PATH (DYLD_LIBRARY_PATH on Mac OS X)
  2. ) the rpath of the executable
  3. ) standard directories

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

This topic: Development > HowToLink
Topic revision: 16 Mar 2016, MartinDiehl
This site is powered by FoswikiCopyright by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding DAMASK? Send feedback
§ Imprint § Data Protection