LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-27-2008, 06:32 PM   #1
bjdodo
Member
 
Registered: Dec 2006
Location: Ireland
Distribution: Slackware 12, kubuntu
Posts: 35

Rep: Reputation: 16
how shared libraries are found


Hello

I have a theoretical question about the linux FS hierarchy and shared libraries.

Sorry, but I come from windows and I think the best way I can ask the question is to explain in windows what I am looking for. In windows there are dlls, in Linux there are blabla.so.version files. If I am not mistaken, these serve similar purposes. In windows, an exe will find a dll if it is in the same directory, or the directory of the dll is in the path.

How does this work in linux? I can see that there are *.so.* files in /usr/local/lib, but the /usr/local/lib is not in the path (on my system).

Is /usr/local/lib hardcoded in the source code? Or is there a separate place to configure the location of shared libraries?

Thank you very much for any answers.

Jozsi
 
Old 03-27-2008, 07:26 PM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
I recently responded to a similar question. Perhaps it will answer your question.
Quote:
When you link programs with libraries, the linker doesn't use the full path to the library; only the library name. The standard libraries to use are /lib and /usr/lib. Any others must be given in the compile command.

At run-time, the system will search only /lib and /usr/lib for the libraries. If they are not found, you get error messages.

To fix that, you need to add "-W1,rpath" option with the path to additional libraries, like so:
g++ -Wall simple_xy_wr.cpp /usr/local/lib/libnetcdf_c++.a /usr/local/lib/libnetcdf.a -W1,rpath,/usr/local/lib.

Now the system should search the standard /lib and /usr/lib, plus /usr/local/lib, at run-time.

Reference: Advanced Linux Programming, ch. 2, section 3.4, Library Dependencies
 
Old 03-27-2008, 07:51 PM   #3
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 371

Rep: Reputation: 30
If you want to run a program which needs a library which is not in the default paths, you can specify $LD_LIBRARY_PATH and $LD_RUN_PATH environment variables. (One of these two... I don't remember now which one... is for running, and other is for compiling.)

For example, I have installed some softwares in a specific directory /tmp/fakeroot (I install everything by compiling it with "./configure --prefix=/tmp/fakeroot"). So I have added following in my bashrc:
Code:
export LD_LIBRARY_PATH="/tmp/fakeroot/lib:$HOME/lib"
export LD_RUN_PATH=$LD_LIBRARY_PATH
You can test whether a program is able to find all the libraries or not by running "ldd <binaryfile>"
 
Old 03-27-2008, 07:52 PM   #4
Bruce Hill
HCL Maintainer
 
Registered: Jun 2003
Location: McCalla, AL, USA
Distribution: Arch, Gentoo
Posts: 6,940

Rep: Reputation: 129Reputation: 129
A simple way to find out what libraries a particular app uses:
Code:
mingdao@silas:~$ which konsole
/usr/bin/konsole
mingdao@silas:~$ ldd /usr/bin/konsole
        linux-gate.so.1 =>  (0xffffe000)
        libkdeinit_konsole.so => /usr/lib/libkdeinit_konsole.so (0xb7e60000)
        libkdeprint.so.4 => /usr/lib/libkdeprint.so.4 (0xb7d95000)
        libkparts.so.2 => /usr/lib/libkparts.so.2 (0xb7d51000)
        libkio.so.4 => /usr/lib/libkio.so.4 (0xb7a17000)
        libkdeui.so.4 => /usr/lib/libkdeui.so.4 (0xb773e000)
        libkdesu.so.4 => /usr/lib/libkdesu.so.4 (0xb7725000)
        libkwalletclient.so.1 => /usr/lib/libkwalletclient.so.1 (0xb7714000)
        libkdecore.so.4 => /usr/lib/libkdecore.so.4 (0xb74df000)
        libDCOP.so.4 => /usr/lib/libDCOP.so.4 (0xb74ad000)
        libresolv.so.2 => /lib/libresolv.so.2 (0xb747d000)
        libart_lgpl_2.so.2 => /usr/lib/libart_lgpl_2.so.2 (0xb7467000)
        libidn.so.11 => /usr/lib/libidn.so.11 (0xb7436000)
        libkdefx.so.4 => /usr/lib/libkdefx.so.4 (0xb740e000)
        libqt-mt.so.3 => /usr/lib/qt-3.3.8b/lib/libqt-mt.so.3 (0xb6d69000)
        libmng.so.1 => /usr/lib/libmng.so.1 (0xb6d08000)
        liblcms.so.1 => /usr/lib/liblcms.so.1 (0xb6cd1000)
        libjpeg.so.62 => /usr/lib/libjpeg.so.62 (0xb6cb1000)
        libGL.so.1 => /usr/lib/libGL.so.1 (0xb6c0c000)
        libXmu.so.6 => /usr/lib/libXmu.so.6 (0xb6bf7000)
        libXt.so.6 => /usr/lib/libXt.so.6 (0xb6ba8000)
        libXrandr.so.2 => /usr/lib/libXrandr.so.2 (0xb6ba2000)
        libXcursor.so.1 => /usr/lib/libXcursor.so.1 (0xb6b99000)
        libXfixes.so.3 => /usr/lib/libXfixes.so.3 (0xb6b94000)
        libXinerama.so.1 => /usr/lib/libXinerama.so.1 (0xb6b90000)
        libXft.so.2 => /usr/lib/libXft.so.2 (0xb6b7e000)
        libfontconfig.so.1 => /usr/lib/libfontconfig.so.1 (0xb6b53000)
        libfreetype.so.6 => /usr/lib/libfreetype.so.6 (0xb6ae5000)
        libexpat.so.1 => /usr/lib/libexpat.so.1 (0xb6ac5000)
        libpng.so.3 => /usr/lib/libpng.so.3 (0xb6a8a000)
        libSM.so.6 => /usr/lib/libSM.so.6 (0xb6a81000)
        libICE.so.6 => /usr/lib/libICE.so.6 (0xb6a6a000)
        libfam.so.0 => /usr/lib/libfam.so.0 (0xb6a63000)
        libpthread.so.0 => /lib/libpthread.so.0 (0xb6a4c000)
        libacl.so.1 => /lib/libacl.so.1 (0xb6a45000)
        libattr.so.1 => /lib/libattr.so.1 (0xb6a41000)
        libz.so.1 => /usr/lib/libz.so.1 (0xb6a2c000)
        libutil.so.1 => /lib/libutil.so.1 (0xb6a28000)
        libXtst.so.6 => /usr/lib/libXtst.so.6 (0xb6a23000)
        libXext.so.6 => /usr/lib/libXext.so.6 (0xb6a15000)
        libXrender.so.1 => /usr/lib/libXrender.so.1 (0xb6a0d000)
        libX11.so.6 => /usr/lib/libX11.so.6 (0xb6926000)
        libxcb-xlib.so.0 => /usr/lib/libxcb-xlib.so.0 (0xb6923000)
        libxcb.so.1 => /usr/lib/libxcb.so.1 (0xb690c000)
        libXau.so.6 => /usr/lib/libXau.so.6 (0xb6909000)
        libXdmcp.so.6 => /usr/lib/libXdmcp.so.6 (0xb6904000)
        libdl.so.2 => /lib/libdl.so.2 (0xb6900000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb681a000)
        libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0xb680e000)
        libm.so.6 => /lib/libm.so.6 (0xb67e8000)
        libc.so.6 => /lib/libc.so.6 (0xb669c000)
        libGLcore.so.1 => /usr/lib/libGLcore.so.1 (0xb5bad000)
        libnvidia-tls.so.1 => /usr/lib/tls/libnvidia-tls.so.1 (0xb5bab000)
        /lib/ld-linux.so.2 (0xb7ef7000)
 
Old 03-27-2008, 07:56 PM   #5
bjdodo
Member
 
Registered: Dec 2006
Location: Ireland
Distribution: Slackware 12, kubuntu
Posts: 35

Original Poster
Rep: Reputation: 16
Thank you very much!!! Your posts did help a lot.

Thanks :-)

Jozsi

Last edited by bjdodo; 03-27-2008 at 07:58 PM.
 
Old 03-27-2008, 09:39 PM   #6
ComputerGreek
Member
 
Registered: Sep 2007
Posts: 152

Rep: Reputation: 30
Quote:
Originally Posted by bjdodo View Post
How does this work in linux? I can see that there are *.so.* files in /usr/local/lib, but the /usr/local/lib is not in the path (on my system).Jozsi
Execute the command:

echo "/usr/local/lib" >> /etc/ld.so.conf

to permanently add /usr/local/lib to your library path.

Many distros like Red Hat deliberately do not do this for you, even though many have complained (since about 1998) to Red Hat etc.
 
Old 03-27-2008, 11:32 PM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Also review the command ldconfig. (man ldconfig)

When Linux is searching for a library, there are two "trusted" places where it knows to look: /lib and /usr/lib. Any other "places to look" must be identified (by the root user) in the file /etc/ld.so.conf.

Since "looking for things" is actually expensive, Linux builds a cache of the search-results so that actual searching is unnecessary. That's what ldconfig does: it rebuilds the cache.

At runtime, it is possible to (as with Windows) declare "search paths" which applications can refer-to ... over-and-above these system-wide caches and search-paths. These are on a user-by-user and app-by-app basis.
 
Old 03-28-2008, 04:19 AM   #8
Bernstein34
LQ Newbie
 
Registered: Mar 2008
Posts: 4

Rep: Reputation: 0
Quote:
Originally Posted by Bruce Hill View Post
A simple way to find out what libraries a particular app uses:
You can also use:

objdump -p MyBinary | grep NEEDED

This is NOT the same as the ldd program, which dumps a list of every single thing loaded into a process by the ELF file, MyBinary, including dependencies of dependencies. Usually, this list of needed files will be much smaller than the output from ldd.

objdump is part of the GNU binary utilities: the most recent stable version being binutils-2.18.tar.bz2

> which konsole

Code:
/opt/kde3/bin/konsole
> objdump -p /opt/kde3/bin/konsole | grep NEEDED

Code:
  NEEDED      libkdeinit_konsole.so
  NEEDED      libkdeprint.so.4
  NEEDED      libkparts.so.2
  NEEDED      libkio.so.4
  NEEDED      libkdeui.so.4
  NEEDED      libkdesu.so.4
  NEEDED      libkwalletclient.so.1
  NEEDED      libkdecore.so.4
  NEEDED      libDCOP.so.4
  NEEDED      libresolv.so.2
  NEEDED      libart_lgpl_2.so.2
  NEEDED      libidn.so.11
  NEEDED      libkdefx.so.4
  NEEDED      libqt-mt.so.3
  NEEDED      libXi.so.6
  NEEDED      libXrandr.so.2
  NEEDED      libXcursor.so.1
  NEEDED      libXinerama.so.1
  NEEDED      libXft.so.2
  NEEDED      libfontconfig.so.1
  NEEDED      libfreetype.so.6
  NEEDED      libexpat.so.0
  NEEDED      libdl.so.2
  NEEDED      libpng.so.3
  NEEDED      libXext.so.6
  NEEDED      libX11.so.6
  NEEDED      libSM.so.6
  NEEDED      libICE.so.6
  NEEDED      libpthread.so.0
  NEEDED      libfam.so.0
  NEEDED      libz.so.1
  NEEDED      libutil.so.1
  NEEDED      libXtst.so.6
  NEEDED      libXrender.so.1
  NEEDED      libstdc++.so.6
  NEEDED      libgcc_s.so.1
  NEEDED      libm.so.6
  NEEDED      libc.so.6

Last edited by Bernstein34; 03-28-2008 at 04:21 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
error while loading shared libraries: libstdc++.so.5: cannot open shared object file: Franziss Linux - Newbie 10 06-28-2010 05:47 AM
Urgent !!! rpm: error while loading shared libraries: libelf.so.1: cannot open shared tinaa Linux - Software 5 12-02-2008 03:19 PM
error while loading shared libraries: libgvc.so.3: cannot open shared object file coolrock Slackware 6 01-17-2007 05:10 PM
error while loading shared libraries: libdb-4.1.so: cannot open shared object file putquery8581 Linux - Software 1 10-01-2004 07:03 AM
mplayer: error while loading shared libraries: libdvdread.so.3: cannot open shared ob Bruce Hill Slackware 6 12-11-2003 08:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration