Search code examples
c++compilation

Compilation process for C++ application?


I have been programming for 2 years now and always have faced difficulty when dealing with the compilation process. I did not study Computer Science during my Engineering, but necessity drove me towards learning C++. I tried understanding the compilation process from some blogs, but they were always in a language I could not understand. So I searched this site for a similar question, but could find none. So I would like to know how the text from a .cpp is converted to a binary executable?


Solution

  • Basically, the preprocessor runs first resolving all your #includes, #defines, etc with simple text substitution. Then the compiler creates a compilation-unit for each .cpp file which pretty much boils everything down to machine-code except for "connections" or linkages between shared data and functions. There may be many levels of optimisation for speed and/or space performed. This is repeated for all your .cpp files. Finally, a link phase ties all these compilation-units and the libraries they use together into an executable.