The difference between .o, .a, .lo and .so.
Summary
- .o is usually a non-PIC object file issued by the compiler (prior to the linker stage). When it is connected with exe, the code will be included in the executable file - we contact the communication time.
- .a is usually an archive library containing one or more .o [non-PIC] files. When connecting to exe, specific "* .o" files in the archive will be inserted into the file.
- .lo is usually a “library object” that contains PIC code compiled manually with gcc -fPIC or libtool.
- .so files are shared object files. They contain PIC objects.
Note:
- If you need static executables, use the ".o" and ".a" files.
- If you need / need dynamic executables to link to libraries at runtime, use .lo and .so files.
Introduction
Although I like the answers above, they do not cover the .a / archived library form. So, here I will talk about all three with the bonus of adding libraries to the .so format. Also, in the spirit of stackexchange, I will use more text if the links are broken (note that I do not need link links for this).
File type .o
When compiling a .o file, it is an object file containing the compiler source code for the target platform. To create a .o file:
gcc -c filename.c <
Note that this example does not create an independent position code (PIC). We are considering this object for possible inclusion in a static library or executable file. That is, when we associate the executable with the .o file, the code in the .o file is inserted into the executable --- it is bound at build time, and not at run time. This means that the executable can be redistributed without including the .o file. Caution: this is an agreement that the .o file is considered non-PIC. Usually we call PIC object files the extension .lo.
File type .a
The .a file type is an archive library. It contains one or more .o files and is commonly used to create static executables.
We use the ar command to manage archive libraries. In the example below, that (1) creates an archive library from .o files, then (2) lists the contents of one.
Create library
$ ls *.o ao bo co <=== the files going in the archive $ ar q libmyStuff.a *.o <=== put *.o files in an archive (or new one) ar: creating libmyStuff.a $ ls *.a <=== just show the library created libmyStuff.a
Display contents of archive library
$ ar t libmyStuff.a ao bo co
File type .lo
Using .lo is a convention that is often used for object-independent object files. In the current directory, the libtool compilation command creates the .lo file and the .o file, one with the PIC code and one without the PIC code. See the following result:
$ libtool compile gcc -c ac libtool: compile: gcc -c ac -fPIC -DPIC -o .libs/ao <== PIC code libtool: compile: gcc -c ac -o ao >/dev/null 2>&1 <== Not-PIC code $ ls a.lo ao a.lo ao <=== a.lo contains the PIC code.
Also note that the .libs subdirectory was created using ao in it. This file is a PIC code, despite the name. Libtool moved this file to the current directory and changed the extension to .lo.
You can always manually create .lo files by simply using the PIC options for gcc when compiling. Move the resulting .o files to the .lo extension.
File type .so
By convention .so, the library file is "shared object". We put PIC object files in shared libraries. In a contract with .o and .a files, when we link to .so files, the code is not included in the resulting compiled file. That is, we use runtime binding (as in the case of .lo). There is more than one form of runtime binding, but we will not go into that.