export LD_LIBRARY_PATH vs -L .

export LD_LIBRARY_PATH vs -L .

par Tugdual Marc-Emmanuel Pierre Marie Kerjan,
Number of replies: 1

The title is self explanatory:

It is my understanding that -L . when compiling specifies that the current working directory is added to the list of directories searched by ld to link shared libraries during compilation, and that this is equivalent to setting the Environment variable for default library path: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/tugdual/Documents/EPFL/gameboy/done/

Yet when executing the command to compile, if we do not set the environment variable, gcc can't seem to find the library, even with the -L . :

  • cc -L .   unit-test-cpu-dispatch.o alu.o bit.o util.o cpu-registers.o cpu-storage.o component.o bus.o memory.o cpu-alu.o opcode.o error.o  -lcs212gbcpuext -lcheck -lm -lrt -pthread -lsubunit  -o unit-test-cpu-dispatch
  • ./unit-test-cpu-dispatch && true
  • ./unit-test-cpu-dispatch: error while loading shared libraries: libcs212gbcpuext.so: cannot open shared object file: No such file or directory
  • Makefile:157: recipe for target 'check' failed
  • make: *** [check] Error 127

So what is the difference?

Thanks,

Tugdual

In reply to Tugdual Marc-Emmanuel Pierre Marie Kerjan

Re: export LD_LIBRARY_PATH vs -L .

par Cédric Hölzl,

"-L", tells the compiler where the library(ies) can be found for compilation

"-l", tells the compiler the name of the library(ies)

It doesn't tell the executable where the libraries are (since the location can change from one system to another)

Then comes a difference:

  • Static library: from a ".o", makes the created executable to be independent of the library (and doesn't require the lib to be distributed with it).
  • Dynamic library: from a ".so", makes the created executable dependent on a library that the user needs to provide to the executable though the environment variable "LD_LIBRARY_PATH", which holds the path to where local dynamic libraries are found. By default thte path doesnt contain "the current directory", hence we need to re-export it.

In our case we use a dynamic library we provided. In many cases dynamic libraries are prefered.