views:

65

answers:

2

Hi,

I have an application that i have built(on linux, written in c++ compiling with g++), that uses shared libs, it works fine when i run it locally, as the shared libs can be found, however when i run it on a cluster where the libs are only installed on the head nodes, i get the error :

./start: error while loading shared libraries:

Is there a compiler switch that would include all the shared libs in the executable and so get around the problem of having to update the whole cluster ?

thanks

Nigel.

+1  A: 

You can't include shared libraries in an executable, so you'll have to link statically to those libs instead.

Do this in your linking stage: -Wl,-static -lyourlib -Wl,-Bdynamic

That should link to yourlib statically, and any other libs dynamically.

nos
+1  A: 

I suppose you indeed want to use shared library and not the static one, so we'll not discuss what is better shared or static library and when use one or another - both have their advantages and disadvantages.

If you know where the libraries are located on machine you're running your application and this location is not changing you can compile as following:

g++ -Wl,rpath,full_path_to_libraries

This will embedded the path to the binary itself. If rpath is not used, compiler will look for environment variable LD_RUN_PATH used at compile time:

export LD_RUN_PATH=full_path_to_libraries (for bash)

g++ .... (compile)

My personal prefer is rpath, since the path is embedded in your code (e.g. makefile) and if you use version source control system, you can always know how what path was used.


There're also alternatives to indicate path to libraries at run time. First one is to set environment variavble LD_LIBRARY_PATH. You can set it in several ways:

  • indicate LD_LIBRARY_PATH in command line itself

    ~: LD_LIBRARY_PATH=full_path_to_libraries my_application

  • set LD_LIBRARY_PATH once in ~/.bashrc, in /etc/profile or in command line (export LD_LIBRARY_PATH=path (for bash)) and then execute your application
  • as superuser add path to libraries in /etc/ld.so.conf file and execute ldconfig utility. After that you can execute your application and your libraries will be located (see man ldconfig)
dimba