Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "object" in "object file" and why is it called this way? [duplicate]

I was asked a question: "What is an 'object file'?".

After looking at Wiki, I only know that it contains objects.

But what are those objects and why someone called them that way?

like image 279
sthlm58 Avatar asked Nov 23 '11 22:11

sthlm58


People also ask

What is in an object file?

An object file is a computer file containing object code, that is, machine code output of an assembler or compiler. The object code is usually relocatable, and not usually directly executable. There are various formats for object files, and the same machine code can be packaged in different object file formats.

Why is it called an object file?

When you have a project with many C files (for instance), you'll compile each one into object code, and then you will link all object files together in order to produce the final product. The term object stands here for sequences of unlinked machine code (basically). An object file contains objects.

What is a file object also known as?

An Object file is the compiled file itself. There is no difference between the two. An executable file is formed by linking the Object files. Object file contains low level instructions which can be understood by the CPU. That is why it is also called machine code.

What are object files used for?

Object files represent some control data with a machine-independent format. making it possible to identify object files and interpret their contents in a common way. The remaining data in an object file use the encoding of the target processor, regardless of the machine on which the file was created.


1 Answers

Object files (or object code) are machine code files generated by a compiler from source code.

The difference with an executable is that the object file isn't linked, so references to functions, symbols, etc aren't defined yet (their memory addresses is basically left blank).

When you compile a C file with GCC:

gcc -Wall -o test test.c 

Here you are compiling AND linking. So you'll got an executable, containing all the memory addresses references for the symbols it contains (libraries, headers, etc).

But when you do this:

gcc -Wall -o test.o -c test.c 

You'll produce and object file. It's also machine code, but it will need to be linked in order to produce an executable, or a library.

When you have a project with many C files (for instance), you'll compile each one into object code, and then you will link all object files together in order to produce the final product.

For instance:

gcc -Wall -o foo.o -c foo.c              // Object file for foo.c gcc -Wall -o bar.o -c bar.c              // Object file for bar.c gcc -Wall -o main.o -c main.c            // Object file for main.c gcc -Wall -o software foo.o bar.o main.o // Executable (foo + bar + main) 

The term object stands here for sequences of unlinked machine code (basically). An object file contains objects.

You asked: why is this call that way. I can't really answer. Why is "blue" named "blue"? ; )

It's just the term used since... well, decades...

For information, the GCC Internals documentation only defines object code as:

The “source code” for a work means the preferred form of the work for making modifications to it. “Object code” means any non-source form of a work.

Pretty vague about the historical reason...

I simply hope you now understand better what is an object file. I think it's more important than knowing why it's called like that, as words are just, well, words...

like image 87
Macmade Avatar answered Sep 27 '22 18:09

Macmade