There are 3 kind of object files.
Contain machine code in a form that can be combined with other relocatable object files at link time, in order to form an executable object file.
If you have an a.c
source file, to create its object file with GCC you should run:
gcc a.c -c
The full process would be: preprocessor (cpp) would run over a.c. Its output (still source) will feed into the compiler (cc1). Its output (assembly) will feed into the assembler (as), which will produce the relocatable object file
. That file contains object code and linking (and debugging if -g
was used) metadata, and is not directly executable.
Special type of relocatable object file that can be loaded dynamically, either at load time, or at run time. Shared libraries are an example of these kinds of objects.
Contain machine code that can be directly loaded into memory (by the loader, e.g execve) and subsequently executed.
The result of running the linker over multiple relocatable object files
is an executable object file
. The linker merges all the input object files from the command line, from left-to-right, by merging all the same-type input sections (e.g. .data
) to the same-type output section. It uses symbol resolution
and relocation
.
When linking against a static library
the functions that are referenced in the input objects are copied to the final executable.
With dynamic libraries
a symbol table is created instead that will enable a dynamic linking with the library's functions/globals. Thus, the result is a partially executable object file, as it depends on the library. If the library doesn't exist, the file can no longer execute).
The linking process can be done as follows:
ld a.o -o myexecutable
The command: gcc a.c -o myexecutable
will invoke all the commands mentioned at point 1 and at point 3 (cpp -> cc1 -> as -> ld1)
1: actually is collect2, which is a wrapper over ld.