user3647289
user3647289

Reputation: 31

Difference in commands to create executables from shared library

What is the difference between the launch and launch1 executable created by the following commands:

gcc main.o ./my_lib/libshared_secure.so -o launch 

and

gcc main.o -L ./my_lib -lshared_secure -o launch1

Here main.o is the object code of the main function and libshared_secure.so is a shared library. I expected that both launch and launch1 would be same but I was incorrect. Why are the two executables different and which part of the above commands causes these differences? The difference was that in executing launch I didn't have to set and export LD_LIBRARY_PATH variable to the path of libshared_secure.so but I had to do that in executing launch1.

Upvotes: 1

Views: 41

Answers (1)

keltar
keltar

Reputation: 18389

readelf -d launch | grep libshared_secure.so

Will report [./my_lib/libshared_secure.so]. However, for launch1 it will be [libshared_secure.so]. Linker will try to load given libraries in system directories and in directories relative to your current working directory (so, if you'll launch binary from some other place (not ./launch but e.g. ../launch from subdirectory) - it will not find library without LD_LIBRARY_PATH).

Other functionality is rpath - linker could take directory to look into, and write it into ELF header, without need to specify LD_LIBRARY_PATH. This avoids problem with current working directory because you could use paths relative to binary directory, not your current one. E.g. gcc -Wl,-rpath='$ORIGIN/my_lib' -Lmy_lib -lshared_secure main.o will link your binary with libshared_secure.so but will add relative entry to rpath.

Upvotes: 2

Related Questions