The -c flag for compilation
What you'll learn: How to stop the compiler halfway through the build process to create reusable object files.
The Halfway Point
Building a C program is usually a two-step dance: compiling (translating code into machine language) and linking (stitching those translations into a final app). By default, when you run gcc main.c, the compiler does both steps instantly. However, as your projects grow, you won't want to re-translate every single file every time you change one line of code.
This is where the -c flag comes in. It tells the compiler: "Translate this source code into machine code, but stop right there. Do not try to make a finished executable yet."
From .c to .o
When you use the -c flag, the compiler produces an object file ending in .o instead of a finished program.
Think of a C file like a recipe. If you are baking a massive wedding cake, you don't bake the whole thing at once. You bake the sponge, mix the frosting, and prep the fillings separately. An .o file is like a single layer of that cake—it is "cooked," but you can't serve it until all the layers are stacked and frosted together.
// math_utils.c
// We compile this with: gcc -c math_utils.c
// This creates: math_utils.o
int add(int a, int b) {
return a + b;
}
Why bother?
If you have a project with 100 files and you change a typo in just one, you shouldn't have to re-translate the other 99. By using -c, you create 100 separate .o files. If you change file #50, you only run -c on that specific file again.
Finally, you tell the compiler to "link" all those existing .o files together into the final program. This is the secret to how massive software like Windows or Chrome can be built efficiently. The -c flag allows for modular building: you prepare the pieces individually so that the final assembly is fast and organized.
Key takeaway
The -c flag stops the build process early to create an object file (.o), allowing you to compile individual pieces of a program without linking them into a final executable.