gcc main.c — From text to program
We’re all familiar with computer programs but how does a program come to life from its original code? Magic right? Well not quite.
If I wrote the above text and saved it in a regular text file, it would remain as just that…text. But suppose I named the file as print_list.c would this change anything? We’re getting closer but we’re not there yet. At this point we have what’s known as source code —a human readable list of instructions that’s written in a computer programming language. There’s many different programming languages that exist with different functionalities but the one used in this example is C.
The next step is to turn our code into an actual functioning program. For that task we’ll need to translate these instructions into something the computer can understand and work with. We need to compile our code. There’s different compilers that exist but for our purposes, I’ll use the one from the GNU Project (https://www.gnu.org).
There’s a lot that’s going on here in the above command. We’re instructing the computer to use the GNU compiler to convert the code in the print_list.c into a program called list_program. Behind the scenes there’s 4 modules that work to accomplish this goal.
- The preprocessor will take the source code file and preprocess it into an intermediary file that will be used in the next step.
- The compiler will then compile the output of the preprocessor and the source code into assembly code.
- Next, the assembler will convert the assembly code created from step 2 into an object file.
- Finally, the linker will then connect our object file with any pre-written code already compiled that we may have used or better known as libraries.
Now that our source code file has been compiled, we can now run our program: