gcc main.c?

What’s happening behind the scenes…

There are a few things that need to happen in order to turn a C program into and executable. When compiling a C program, the code will be put through a four step process consisting of preprocessing, compilation, assembly and linking. Each step takes the code through a unique process, which is covered in detail below…

First step: Preprocessing!

During the preprocessing step, lines are run through the preprocessor and interpreted as preprocessor commands. Once these commands are defined, a macro language is created with its own syntax. A few things are happening with our code at this point:

  • comments are removed
  • macros are expanded
  • included files are expanded (headers)

The resulting file is saved in the filename.i format.

Second step: Compilation!

Our file needs some assembly level instructions… Time to compile! The filename.i file is output in the filename.s intermediate compiled output, and is in assembly level instructions for the machine, which brings us to the third step…

Third step: Assembly!

Our filename.s is converted to an intermediate filename.o to provide machine level instructions. This is known as the object file. Existing code will be converted into machine level language, but function calls are not resolved.

Fourth step: Linking!

Time to link function calls with definitions! The linker will replace the place-holders for the functions with the actual addresses for the functions. The linker will also add the necessary code to start and end the program, as well as standard code that is necessary in order to return the return value of the program.

So there we have it! The process that a C program goes through to become an executable.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.