How to store object file name in Linux

An object file in Linux is a computer-generated file that contains the object codeThe output from the assembler or a compiler, usually in machine code language.. Generally, object code is not directly executed and must be called using a command in order to execute.

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.

Methods of creating object filenames

Object files in Linux contain the .obj or .o extension. Let’s look at how to create them in the illustration below:

How is object file created
How is object file created

Direct

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.

Taking object file input from the user

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

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved