91

As far as I understand they are libraries, but what is the difference between the two?

Erik B
  • 1,103
  • 1
  • 10
  • 9
  • Related post - [What's the difference between .so, .la and .a library files?](https://stackoverflow.com/q/12237282/465053) – RBT May 21 '18 at 23:43

2 Answers2

73

A .a file is a static library, while a .so file is a shared object (dynamic) library similar to a DLL on Windows. There's some detailed information about the differences between the two on this page.

ajk
  • 914
  • 7
  • 4
  • 29
    `.a` can only be included as part of a program during compiling. `.so`'s can be "imported" while a program loads. – LawrenceC May 14 '11 at 23:39
  • 5
    what does .a stand for? – hfrmobile Nov 29 '16 at 12:17
  • 9
    @hfrmobile The a stands for *archive* - a static library is a collection of object files created using the `ar` utility. [More info here](http://www.tldp.org/HOWTO/Program-Library-HOWTO/static-libraries.html) – ajk Nov 29 '16 at 16:19
36

As a follow on, a .a file is an "ar" archive. Not unlike a tar archive, it stores .o or object files, allowing them to be pulled out of the archive, and linked into a program, among other things. You could use ar to store other files if you wanted.

You can get a listing of the members of an ar file with the -t parameter, for instance:

ar -t /usr/lib/libc.a

A .so file is a "shared object" file, and has a lot more information available to the linker so that members can be linked in to a loading program as rapidly as possible.

For instance, try:

objdump -T /lib/libc-2.11.1.so

(or whatever version of libc.so you have in your /lib directory.) Note that a .so file could also just contain a linker script directing it to find the file elsewhere, or use something else.

Interestingly, a .so file can also be a full fledged program. For instance, trying running /lib/libc.so.6. (This works on my Ubuntu 20.04 system)

Hack Saw
  • 1,004
  • 7
  • 14
  • Typo. you mean to write `ar -t /usr/lib/libc.a`. Actually the `-` doesn't seem to be necessary in this case. Also `/lib/libc.so.6.` just prints some output. I don't know if I would call it a `full fledged program`. – Faheem Mitha May 15 '11 at 08:09
  • 1
    It's a full fledged program in the sense that it has a main symbol for exec to find. – Hack Saw May 16 '11 at 16:05
  • 3
    You can also run `ldd` on .so's, and it will show you what other libraries it uses. Static will return with a message saying it's not a dynamic library. – Marcin May 17 '11 at 01:06