What happens when you type gcc main.c?

Mohameth Seck
4 min readFeb 7, 2019

A computer programming is the process of developing and implementing various sets of instructions to allow the computer to perform a specific task. These commands are computer programs and help the computer function smoothly. The programming languages used to write these programs are not understood to the untrained eye and a computer would not understand it either if it was not in machine code, i.e. in the form of 1s and 0s. It would be a tedious task to write a computer program in machine code and that is why we have programming languages like Java, C++, python etc. Programs written in these languages are called source code. The source code can not be directly executed by the computer and must be translated in machine code to be executed. This is where a software that translate these programs comes into play

Interpreted Language vs Compiled Language

What is the difference between a compiled and interpreted language? Well they both are different ways to execute a program written in a programming language or script langauge. A compiler takes the entire program and turns it into object code. This code is known as binary code and can executed by the computer. An interpreter will directly execute instructions by a script or programming language without converting it into object code or machine code.With interpreters it takes less time to analyze, longer to execute. A compiler takes longer to analyze, but overall execution time is much faster. Programming languages like C and C++ use compilers.

What is GCC in C programming?

GCC in capitals is the abbreviation for the GNU Compiler Collection and it supports languages like C and C++. Now in all lower case, it is the GNU C Compiler. It will compile your C code making it object code, also called machine code. Now the GCC compiler is a translator software tool to make human readable code into machine code. A translator is also referred to as a compiler, assembler, and linker.

A compiler, as mentioned earlier in the reading, converts high level language code into machine code. They can take a while because the have to turn high level code to low level machine code. They do however execute a lot faster. Interpreters are another way to get your code to run on your processor, but it is not the same as a compiler. An interpreter translates code also, but reads the code and immediately executes on that code, making it faster than a compiler. You’ll see interpreters being used in software development debugging tools because of how fast they are. However, because they skip the compilation step at the start of the entire program, it is much slower to execute than the same program that was compiled.

An assembler translates code written in assembly language into machine code. Assembly language is a low-level programming language and can be hard to understand at a glance.

A linker links the object code created by the assembler with library functions we use when write our program. A linker can merge multiple C files by compiling them into one executable file and link our code with library function code. The linker will decide to link as static or dynamic. After all that it will pack all our code into a single file, .exe file in Windows and a.out in Linux, so that we can run our program in the computer.

Now that we got all that out the way, what does happen when you type gcc main.c?

After we create our source code we have to execute it. We do this by using the gcc main.c command in our terminal. Now there are a few steps to obtain the executable where the source code will ubdergo conversion to have the file generated.

The stages are:

  • Pre-processor
  • Compiler
  • Assembler
  • Linker

So it reads the source file, processes it, and links it with a runtime library. The file that is being compiled is main.c in this case. When the source code is sent through the pre-processor where the code is expanded, comments are removed, and Marcros are replaced. The compiler stage compiles files made by the preprocessor as input and that generates assembly code so our C file can be turned into assembly langauge. Computers can only read machine code, binary code, remember? Which is why assembly is in the format it needs to be. The assembler will then convert the code into object code. Linker, the last module before a file is executable, generates the a.out which is the executable format for the last compiled C program. To run an executable file we have to type the command ./a.out or ./file_name. This is everything that goes on under the hood when you run the command gcc main.c.

--

--

Mohameth Seck

Studying Software Engineering at Holberton School, New Haven, CT. Learning iOS development on the side. VR/AR enthusiast. I just want to make dope stuff 🤷🏾‍♂️