6

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)
Ethan Heilman
  • 16,347
  • 11
  • 61
  • 88

2 Answers2

6
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
Bob
  • 811
  • 6
  • 10
  • Do I need root to update that file? I want to static link this library. – Ethan Heilman Apr 28 '10 at 02:17
  • ls -l /etc/ld.so.conf to see its permission. It's usually only writable by root. If you want to statically link the library, that's a completely different topic. Everything in my post was about dynamic linking. If you want to go static, use the -static argument to gcc. There are numerous gotchas that come along with static linking as it doesn't always do what you think it's doing. :) – Bob Apr 28 '10 at 02:22
  • Yes, static linking seems to have broken everything =( – Ethan Heilman Apr 28 '10 at 02:45
  • Is there anything else (other than -L and -I) I should be providing to 'gcc -static' so that is can find the libraries? – Ethan Heilman Apr 28 '10 at 03:04
  • http://stackoverflow.com/questions/725472/static-link-of-shared-library-function-in-gcc He said it better than I would have. – Bob Apr 28 '10 at 17:43
4

Provide rpath flag while compiling.

g++ -Wall -o example.out -I ./include/ -L ./examplelibPath -Wl,-rpath ./libPath -l examplelibrary example.cpp
Prashant
  • 121
  • 1
  • 3
  • I used `-Wl,-rpath .` and it solved my problem. Short, sweet, easy. Simpler than editing the /etc/ld.so.conf file. – Mike S Aug 15 '19 at 16:50