What is inside a .o file
What you'll learn: Understand that object files are the "half-baked" machine code translations of your C source code.
The Half-Baked Cookie
When you run a compiler like gcc, it doesn’t jump straight from your text file to a finished program. It stops at an intermediate step: the object file (usually ending in .o or .obj).
Think of your C code as a handwritten recipe. The compiler reads that recipe and performs the heavy lifting of translating it into machine code—the raw 1s and 0s that a CPU understands. However, a .o file is like a half-baked cookie. It contains the right ingredients and has been through the oven, but it isn’t a complete dessert yet because it’s missing the connections to other parts of the meal.
What's actually in there?
An object file consists primarily of three things:
- Machine Code: The binary instructions for the functions you wrote in that specific
.cfile. - Data: Global variables or constants you defined.
- The Symbol Table: This is a "map" of the file. It lists the names of functions defined inside the file and, crucially, a list of "holes" where you called functions that exist somewhere else (like
printf).
A Concrete Example
Imagine you have a file named math_tools.c:
// math_tools.c
int add(int a, int b) {
return a + b;
}
int calculate() {
return add(5, 10);
}
When you compile this into math_tools.o, the file contains the binary logic for add and calculate. If calculate used a function from a different file, the .o file would simply contain a "placeholder" or a note saying: "I need to jump to a function named 'ExternalFunc' here, but I don't know where it lives yet."
Why not just go straight to the program?
We create .o files so we don't have to re-translate the entire project every time we change one line of code. If you have 100 files and change one, you only re-compile that one into a .o file. The final step—linking—then stitches all these separate .o files together into one final executable.
Key takeaway
A .o file is a binary file containing machine code and a "shopping list" of symbols that still need to be connected by the linker.