Jun 14, 2015

C/C++ Shared Vs Static Library

Sometimes you use the library.a, and others the library.so file, but what is the difference between them? why there are two types? Here you can see an overview about them.

Static libraries (.a files) are libraries compiled with all its dependency code inside, in other words, they are independent libraries, that will not use any other external dependency library. Due to this, they are good for compatibility purposes, because if your system has something wrong, incompatible, missing or a corrupt dependency library, doesn't matter! In the other hand, they are bigger than the shared version, and can't use the newer versions of its dependencies libraries.

Shared libraries (.so files), are libraries that depends of others external libraries, and if your system doesn't has some of them, has a incompatible version or a corrupt dependency, then your library will not work. However, they are smaller than static libraries and allow the use of newer versions of its dependencies.

Look the size difference between the static and shared version of the "libc" library for example:

ls -lh libc.a libc.so
-rw-r--r-- 1 root root 3,6M Dez  4  2014 libc.a
-rw-r--r-- 1 root root  283 Dez  4  2014 libc.so

So what is the best? depends of your application! want run without care about the target operating system libraries? space is not the problem? wish performance? use static libraries! Want keep an updated library, small and the target operating system is safe? use shared libraries!

0 comentários :

Post a Comment