Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between .o, .a, and .so files?

I know .o are object files, .a are static libraries and .so are dynamic libraries? What is their physical significance? When can I use some and when not?

like image 520
Utkarsh Srivastav Avatar asked May 12 '15 09:05

Utkarsh Srivastav


People also ask

What is the difference between .a and .so file?

A . a file is a static library, while a . so file is a shared object dynamic library similar to a DLL on Windows.

What does a .so file do?

An SO file is a shared library used by programs installed on the Linux and Android operating systems. It contains common program functions and logic that multiple programs require access to.

What are .a files in Linux?

In general, libraries are collections of data and functions written to be reused by other programmers or programs. On Linux, archive libraries end with the . a extension, and shared object libraries end with the . so extension.

What is in a .o file?

An O file is a compiled C program object. Some C compilers create O files during the executable (. EXE) creation process. O files themselves are typically not executable.


2 Answers

.a is an "archive". Although an archive can contain any type of file, in the context of the GNU toolchain, it is a library of object files (other toolchains especially on Windows use .lib for the same purpose, but the format of these is not typically a general purpose archive, and often specific to the toolchain). It is possible to extract individual object files from an archive which is essentially what the linker does when it uses the library.

.o is an object file. This is code that is compiled to machine code but not (typically) fully linked - it may have unresolved references to symbols defined in other object files (in a library or individually) generated by separate compilation. Object files contain meta-data to support linking with other modules, and optionally also for source-level symbolic debugging (in GDB for example). Other toolchains, again typically on Windows, use the extension .obj rather than .o.

.so is a shared object library (or just shared library). This is dynamically linked to an executable when a program is launched rather then statically linked at build time. It allows smaller executables, and a single object library instance to be used by multiple executables. Operating system APIs are typically shared libraries, and they are often used also in GNU for licensing reasons to separate LGPL code from closed-source proprietary code for example (I am not a lawyer - I am making no claims regarding the legitimacy of this approach in any particular situation). Unlike .o or .a files, .so files used by an application must be available on the runtime system. Other systems (again typically Windows) use .dll (dynamic link library) for the same purpose.

It is perhaps useful to understand that .o files are linked before object code in .a files such that if a symbol resolution is satisfied by a .o file, any library implementation will not be linked - allowing you to essentially replace library implementations with your own, and also for library implementations to call user-defined code - for example a GUI framework might call an application entry-point.

like image 138
Clifford Avatar answered Oct 20 '22 04:10

Clifford


Static libraries are archives that contain the object code for the library, when linked into an application that code is compiled into the executable.

Shared libraries are different in that they aren't compiled into the executable. Instead the dynamic linker searches some directories looking for the library(s) it needs, then loads that into memory. More then one executable can use the same shared library at the same time, thus reducing memory usage and executable size. However, there are then more files to distribute with the executable. You need to make sure that the library is installed onto the user's system somewhere where the linker can find it, static linking eliminates this problem but results in a larger executable file.

like image 24
Parvin Kumar Avatar answered Oct 20 '22 05:10

Parvin Kumar