An object file in Linux is a computer-generated file that contains the
Along with the machine code from the compiler, object files also contain some sort of metadata like the compiler version, compilation date and time, any program symbols, etc.
In this shot, let’s see how an object file is created and its relevant naming convention.
Object files in Linux contain the .obj
or .o
extension. Let’s look at how to create them in the illustration below:
Any source code file can be compiled to create an object file. You likely noticed that an object file main.o
is also created when compiling your main.c
or main.cpp
in C or C++. In Linux, however, we must compile a source code file using the terminal. We use the following command to do so:
$ gcc -c main.c
This produces an object file main.o
, as this command produces an object file by the same name as the source code file. Likewise, if you type the following:
$ gcc -c mycode.c
The object file contains the name mycode.o
.
In the previous example, we saw the gcc
parameter of -o
define the object file that was not used. gcc
has a parameter of o
, which we can use to inform the compiler what we want to name our object file. Therefore, we can also define an object file name using -o
in the gcc
command.
Have a look at the following syntax, which shows how we can use o
. You can see that object file name obj.o
will be the name of our object file after compilation.
gcc -o obj.o -c main.c