What are shared libraries in linux

Sarath Pillai's picture
Shared library in linux

I am quite sure that if a person is very much involved with linux in day to day job, then he or she might have encountered the below error many times.

so what is that error saying? its saying that i need a shared library called libelf.so.1 to run and i cant find it in the specified path.

Now before getting inside into the error lets understand what exactly is shared libraries?

When a linux machine is under operation there are many programs that runs together at a time. Now Some programs might require the capability to read a file from the disk, some might require the capability to print something on the screen, and a lot of other things.

Now when a linux program runs, many of the function that is necessary for the program to run are provided by the system libraries. For example one of the programme requires the function to read a file from the disk, now the programmer doesnt need to write the source code to read a file from the disk, instead the program will call a system's dynamic library whenever required.

Now there are two kind of libraries.

1.Statically Linked Libraries

2.Dynamicly linked libraries

Statically Linked Libraries:

A prgrame that contains the executable code of a certain function, for example the function to read something from the disk as explained above(a programe that does not require a .so file for a certain function because its code itself has the function's source code) is called as staticaly linked libraries

Now statically linked programes do have some demerits i must say, like the following.

1.These programes tend to get large, because they contan all library function's source code.

2.Also in a way RAM is wasted as many running programes have the same function source code in them.

Dynamically Linked Libraries:

many function libraries are already made, and are available to all programes to use whenever required. We call them dynamically linked, they use the same libraries and do not contain the source code for them. So multiple programes uses the same libraries saving RAM and programes become smaller in size.

But there is still an issue with dynamically linked programes.

it wont run untill and unless it has all shared libraries required by it.

Now there is a command in linux(most of you are already familier with that command), which can be used to check which all libraries are required by a particular programe to run. The command is called "ldd" and can be used as below.

Note:  ldd will only give the required shared libraries when the executable is passed as an argument, not the command for example, the below command will not give you the output as expected.

because am in root's home directory and giving rpm as an argument to ldd, so ldd expects that rpm executable to be in that directory or give absolute path.

Now what i did to create that first error in the tutorial was to move one of the .so file required by rpm binary to root;s home directory.

there is something called as Shared object dynamic linker or call it ld.so, which always looks for dependancies whenever an executable file is loaded. And if ld.so fails to find a required library it throws an error.

Now what if you got a set of libraries in some folder and you want ld.so to look there also, for dependancy libraries whenever an executable is loaded.

For that you need to add that directory's full path in the file

/etc/ld.so.conf

You can get the list of all directories and libraries currently in cache where ld.so looks for libraries by the below command.

ldconfig -p

the output of the command will be something like the below.

Now after adding the required folder containing the libraries in /etc/ld.so.conf you can rebuild the cache by the below command.

ldconfig

Hope you enjoyed the post.

Rate this article: 
Average: 3.8 (13 votes)

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.