Search code examples
c++ccompilationsystem

How is the process from compiling a c/c++ code to executing it?


I'm a student of computer science and I have a couple of questions related to the process of compiling a c/c++ code. One question is, when you compile, does it get compiled to assembler or machine code? At the same time, is this assembler/machine code a version for the operating system? The reason of my last question is that when you compile, your program can be executed in different cpus in the same operating system. Apart from that your program can't be executed in another operating system. This make me wonder what there is in the executable you generate for an operating system and what the operating system do with it in order to execute it in different cpu. Thanks in advance and sorry for so many questions.


Solution

  • I think you may be confused about some basics of the process. The compilation process is illustrated nicely in section 1.6 here: https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html

    Your compiled code will be for a specific operating system and cpu architecture. Read this: https://softwareengineering.stackexchange.com/questions/251250/why-do-executables-depend-on-the-os-but-not-on-the-cpu