Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an executable which needs to link with dynamically,so I put them in the same directory,then :

cd path_to_dir

But got this:

error while loading shared libraries: cannot open shared object file: No such file or directory

How can it be unable to find which is already in the same directory as the executable itself?

share|improve this question
up vote 13 down vote accepted

The loader never checks the current directory for shared objects unless it is explicitly directed to via $LD_LIBRARY_PATH. See the man page for more details.

share|improve this answer
echo $LD_LIBRARY_PATH is empty on my machine:( – linuxer Jun 10 '11 at 8:58
It usually is​. – Ignacio Vazquez-Abrams Jun 10 '11 at 8:58
So ld can work well without $LD_LIBRARY_PATH,what's it for then? – linuxer Jun 10 '11 at 8:59
It specifies additional directories for the loader to look in for libraries. – Ignacio Vazquez-Abrams Jun 10 '11 at 9:00
Paths in *nix are separated by a colon (:), not a semicolon. – Ignacio Vazquez-Abrams Jun 10 '11 at 9:18

While you can set LD_LIBRARY_PATH to let the dynamic linker know where to look, there are better options. You can put your shared library in one of the standard places, see /etc/ (on Linux) and /usr/bin/crle (on Solaris) for the list of these places

You can pass -R <path> to the linker when building your binary, which will add <path> to the list of directories scanned for your shared library. Here's an example. First, showing the problem:


void hello_world(void);


#include <stdio.h>
void hello_world(void) {
  printf("Hello world, I'm a library!\n");


#include "libtest.h"
int main(int argc, char **argv) {

Makefile (tabs must be used):

all: hello
%.o: %.c
        $(CC) $(CFLAGS) -fPIC -c -o $@ $< libtest.o
        $(CC) -shared -Wl,-soname, -o libtest.o
        ln -s $< $@
        rm -f hello libtest.o hello.o

Let's run it:

$ make
cc  -fPIC -c -o libtest.o libtest.c
cc -shared -Wl,-soname, -o libtest.o
ln -s
cc     hello.c   -o hello
$ ./hello 
./hello: error while loading shared libraries: cannot open shared object file: No such file or directory

How to fix it? Add -R <path> to the linker flags (here, by setting LDFLAGS).

$ make clean
$ make LDFLAGS="-Wl,-R -Wl,/home/maciej/src/tmp"
cc   -Wl,-R -Wl,/home/maciej/src/tmp  hello.c   -o hello
$ ./hello 
Hello world, I'm a library!

Looking at the binary, you can see that it needs

$ objdump -p hello | grep NEEDED

The binary will look for its libraries, apart from the standard places, in the specified directory:

$ objdump -p hello | grep RPATH
  RPATH                /home/maciej/src/tmp

If you want the binary to look in the current directory, you can set the RPATH to $ORIGIN. This is a bit tricky, because you need to make sure that the dollar sign is not interpreted by make. Here's one way to do it:

$ make CFLAGS="-fPIC" LDFLAGS="-Wl,-rpath '-Wl,\$\$ORIGIN'"
$ objdump -p hello | grep RPATH
  RPATH                $ORIGIN
$ ./hello 
Hello world, I'm a library!
share|improve this answer

To load the shared objects from the same directory as your executable, simply execute:

$ LD_LIBRARY_PATH=. ./binary

Note: It will not modify the LD_LIBRARY_PATH variable of your system. The change only affects to this, and only this, execution of your program.

share|improve this answer
thanks, finally what I need! – Blub Mar 15 at 13:03

For anyone that still struggles without an answer I found one myself with the following suggestion:

You could try updating the using: sudo ldconfig -v

Worked for me.

share|improve this answer
thanks Ian. You saved so much of my time. – praveen Jan 4 at 10:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.