What happens when I run "gcc file.c" in my terminal?
María "Kaky" Rodríguez Petrella
UX Researcher | Social Communicator | UX Writer | Women Techmaker Ambassador | Google Developer Group Medellín | Friends Of Figma Medellin
When writing your code in compiled languages you must perform an action called "Compile". This action allows you to translate the programming code into executable code. There are many ways to do it, depending on the compiler you use, but today I will talk about a specific one: GCC.
But before, let's talk about some other concepts that you'll need to know to fully understand more about the
- The Preprocessor
- The Assembler
- The linker
The preprocessor
It is the first program that calls the compiler and processes some things like #include, #define e #if. These directives are not just for C and can be used with any type of file.
As you can see in the example, the preprocessor replaces the line #include <stdio.h> with the system header file with that name. That is, where the #include directive is, it will be replaced by the contents of the 'stdio.h' file.
The assembler
Most computers have a set of basic instructions that allow you to do simple operations. The assembler is in charge of taking the basic instructions that you give him and transforming them into bits so that the computer's processor can perform the command. This sequence of instructions is also called "source code".
The assembler is in charge of translating to "machine language", that is 0 and 1 or binary language, so the machine can understand exactly what you want it to do.
The linker
Linkers do a very important job, although they usually go unnoticed. They are in charge of converting your code written in a high-level language to an executable that understands the system.
When we want to convert a .c file into an executable we follow this process: First, the preprocessor generates an intermediate file, then you use the C compiler that produces a .s file (this is the assembler code). After this, the assembler reads the .s and converts it into an .o file and this is finally processed by the linker to create an executable on your machine.
Simple, isn't it?
Now that you know all these basics concepts that are part of the C Language Compiler, then you are ready to learn what is GCC and what can you do with it!
What is GCC?
GNU Compiler Collection or GCC is an integrated compiler of the GNU project for C, C++, Objective C, and Fortran. This compiler receives a source file (the code) in any of these languages and generates an executable binary program in the language of the machine where it will run.
How do I run it?
To use this compiler, you must type the following command in your terminal:
This means: Compile the .c file with the latest changes made and the output of it, turn it into a file with the name file_name. This filename is assigned by you.
Important: As a good practice it is advisable to write a different name, than the .c file, for the new file since this way you will have a better record of what you are doing where you are working.
Once you have compiled it, run the "ls" command to verify that the new executable file you designated was indeed created and run it to confirm that the code you made in the .c file worked. Use this command:
Important: Every time you make a change in your code it is imperative that you compile everything again. You can use the same name as the file you already created before if the changes are on the same code. Doing this will only rewrite the information you have in the executable file.
Did it work? Great! Now you can upload it to your repository and continue working on your next project.
Software Developer & Mechanical Engineer
4 年I loved your article! great Job!!