Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting 'error while loading shared libraries' when using -L to specifically find the library

I've been trying to solve this for a few hours now. I am compiling some c files using gcc. The files require libpbc, so I am using the -L flag to point gcc at the directory which contains libpbc.so.1. The code compiles without error yet when I attempt to run it I get the following error message:

./example.out: error while loading shared libraries: libpbc.so.1: cannot open shared object file: No such file or directory

Looking at similar questions this error message seems to indicate that gcc can't find libpbc.so.1. I know gcc sees libpbc.so.1 because when I rename libpbc.so.1 to something else it fails to compile.

I am using -L to point to the directory which contains libpbc.so.1.

Not sure what next steps I can take to figure this out. Would appreciate any ideas. What does this error message mean exactly?

EDIT

Running ldd example.out results in:

linux-gate.so.1 =>  (0xb7fe3000)
libpbc.so.1 => not found
libgmp.so.3 => /usr/lib/libgmp.so.3 (0xb7f87000)
like image 795
Ethan Heilman Avatar asked Apr 28 '10 01:04

Ethan Heilman


People also ask

Where are shared libraries loaded?

Shared Libraries are loaded by the executable (or other shared library) at runtime.

How do I find shared libraries in Linux?

In this standard, folders /lib, /usr/lib and /usr/local/lib are the default folders to store shared libraries. The /lib folder has libraries used during the boot time of the system but also used by programs in the /bin folder. Similarly, the/usr/lib folder has libraries used by programs in the /usr/bin folder.

How do I install a shared library?

Once you've created a shared library, you'll want to install it. The simple approach is simply to copy the library into one of the standard directories (e.g., /usr/lib) and run ldconfig(8). Finally, when you compile your programs, you'll need to tell the linker about any static and shared libraries that you're using.

Do shared libraries need executable?

9.2. As shared libraries cannot be directly executed, they need to be linked into a system executable or callable shared object. Hence, shared libraries are searched for by the system linker during the link process. This means that a shared library name must always start with the prefix lib and have the extension .


2 Answers

ldd example.out

That will give a lot of useful information about dynamic linking. More specifically though, your problem most likely lies with the path of the library not being in.

/etc/ld.so.conf

Note, that if you update that file, you must then run

ldconfig -v
like image 92
Bob Avatar answered Sep 28 '22 15:09

Bob


Provide rpath flag while compiling.

g++ -Wall -o example.out -I ./include/ -L ./examplelibPath -Wl,-rpath ./libPath -l examplelibrary example.cpp
like image 38
Prashant Avatar answered Sep 28 '22 15:09

Prashant