You are not logged in.

#1 2009-04-03 12:37:51

tntcoda
Member
Registered: 2007-07-24
Posts: 115

Shared Library Loading

Hi,

In Windows, you can put an exe's DLLs in its directory, and they get auto loaded at runtime because they are in the same directory as the exe.

Is anything similar possible with Linux binaries? What I want is a portable (not statically linked) application binary (click and run) with all its shared libraries in the same location as the binary (not in /usr/lib or wherever) could this be done so they get loaded at runtime correctly?

Take an application like Firefox, I've read its a nightmare to statically compile, so if I wanted to make it portable as described above (so all its dependencies are in the binary's directory) how would this be done?

As far as I can see I could:

- Play with LD_LIBRARY_LOAD variable and put the binarys directory there and hope they get loaded, doesnt seem a very solid idea
- Modify the source code to load the specific library's up at runtime, could be tricky.

Any other ideas on how this could be done? Or is static compiling the only way forward.

Offline

#2 2009-04-07 13:25:57

krigun
Member
From: Norway
Registered: 2005-06-06
Posts: 122
Website

Re: Shared Library Loading

You can set the LD_LIBRARY_PATH variable, but this generally not considered good practice.

tntcoda wrote:

In Windows, you can put an exe's DLLs in its directory, and they get auto loaded at runtime because they are in the same directory as the exe.

Effectively, this is the same as passing --rpath=\$ORIGIN/. to the linker. Your shared libraries will then be loaded from the current folder of where your executable is.

Ex: Compile file main.cpp, and look for libraries in ../lib folder, relative to your executable.
Folder structure:

my_app/
  bin/
  lib/

Compile and link:

g++ -c -o main.o main.cpp
g++ -Wl,--rpath=\$ORIGIN/../lib -z origin -o main main.o

I believe this works quite well if you wish to distribute a [closed source] application that relies on some LGPL libraries, which I assume is why you are asking such a question.

Last edited by krigun (2009-04-07 13:32:33)

Offline

Board footer

Powered by FluxBB