99

I have build some libraries from sources, and the files after make install are in /usr/local/lib

For example, in my case I have the file libodb-2.2.so which is in this directory.

However when I launch the executable that has linked with libodb, I got the error: error while loading shared libraries: libodb-2.2.so: cannont open shared object file: No such file or directory.

Does it mean that I have build my executable not correctly ? or should I indicate the system that there may be some interesting libs in the folder /usr/local/lib also ?

I'm using Ubuntu 12.04, Linux kernel 3.2.0-38-generic.

  • http://askubuntu.com/questions/165027/why-cant-my-program-find-the-libraries-in-usr-local-lib , http://stackoverflow.com/questions/17889799/libraries-in-usr-local-lib-not-found – Ciro Santilli OurBigBook.com Aug 21 '16 at 17:16
  • I strongly suggest you making good friendship relation with your debugger – DDS Feb 12 '19 at 15:32
  • 6
    @DDS I suggest that you elaborate a bit more. You know it's a collaborative site here. Please, indicate the way gdb would have helped in anyway with a link problem, for example. I am genuinely curious. At first your remark seems to me unrelated to the problem. But I may be wrong. Indeed I do not know enough gdb, and the loading of libraries. – Stephane Rolland Feb 12 '19 at 16:02
  • Sorry... this comment should have been placed on stackoverflow on a learner's question about C programming... just didn't seen was U&L – DDS Feb 12 '19 at 16:10
  • 2
    @DDS Beginner at learning C programming, or at learning building on Linux? Please, be more precise. For example I was coming from Windows. That does not mean I don't know C++... I tend to think your judgmental behaviour is not accurate. Even if it had been on SO, your behaviour will soon get the attention of moderators. Be aware of that. You are on a COLLABORATIVE site, of enthusiast programmers sharing useful knowledge. Not somewhere design to flatter your ego. (Don't worry I have also been gently /reasonably bashed when I started SO, and I think that was a really good thing that happened). – Stephane Rolland Feb 12 '19 at 16:20
  • 1
    C programming: he asked why his code were stopping suddently. https://stackoverflow.com/questions/54653523/check-that-command-line-input-is-numeric/54653550#comment96098874_54653550 – DDS Feb 12 '19 at 16:24

2 Answers2

88

For the current session you can

export LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib

or to make the change permanent you can add /usr/local/lib to /etc/ld.so.conf (or something it includes) and run ldconfig as root.

If you're still having problems, running ldd [executable name] will show you the libraries it's trying to find, and which ones can't be found.

Flup
  • 8,145
  • 5
    From the man page for ldd(1) "In the usual case, ldd invokes the standard dynamic linker (see ld.so(8)) with the LD_TRACE_LOADED_OBJECTS environment variable set to 1, which causes the linker to display the library dependencies. Be aware, however, that in some circumstances, some versions of ldd may attempt to obtain the dependency information by directly executing the program. Thus, you should never employ ldd on an *untrusted executable, since this may result in the execution of arbitrary code. A safer alternative when dealing with untrusted* executables is: ..." – SlySven Apr 07 '16 at 22:04
  • 5
    "... $ objdump -p /path/to/program | grep NEEDED" – SlySven Apr 07 '16 at 22:05
  • Nice one -- I've never come across objdump before. – Flup Apr 07 '16 at 22:32
  • As per this article this answer is canonically incorrect and harmful. For a correct approach the use of -L and -rpath during compile to set the linking search path and runtime search path for libraries is the correct way, as per this question and answer. – Cliff Armstrong Aug 28 '19 at 09:59
  • I see libc.conf have it desired path /usr/local/lib, then ldconfig solve it to me. – EsmaeelE Mar 19 '20 at 00:19
  • What I don't understand is why an executable in /usr/local/bin would not be able to locate a library in /usr/local/lib without modification to environment variables or configurations. What logical reason is there for this? I mean I can execute it from the terminal without adding /usr/local/bin to the path, so why shouldn't it be able to find a library in /usr/local/lib? – Brandon Miller Jul 06 '20 at 14:14
65

If you've already run ldconfig after building the library, keep reading. If not, read aboout ldconfig first.

/usr/local/lib might not be in the library path that ldconfig uses. You can just do this:

ldconfig /usr/local/lib

And the stuff should be added to the linker cache, but it is probably better to add the path properly. Make sure you have a /etc/ld.so.conf.d directory. If so add a file (call it "usr-local.conf", or whatever), and put one line in it:

/usr/local/lib

Now run ldconfig. If you don't have the ld.so.conf.d directory, you should have a /etc/ld.so.conf file and you can add that line to the end of it.

goldilocks
  • 87,661
  • 30
  • 204
  • 262