Regarding C programming, we can now briefly discuss the process of compilation.
Its purpose is to provide an intuitive way for humans to provide instructions that can be easily converted into machine code that is comprehensible to a microprocessor. The compiler is what takes this code, and translates it into the machine code.
To those new to programming, this seems fairly simple. A naive compiler might read in every source file, translate everything into machine code, and write out an executable. This could work, but has two serious problems. First, for a large project, the computer may not have enough memory to read all of the source code at once. Second, if you make a change to a single source file, you would rather not have to recompile the entire application.
To deal with these problems, compilers break their job down into steps; for each source file (each
.c
file), the compiler reads the file, reads the files it references with #include
, and translates it to machine code. The result of this is an "object file" (.o
). Once every object file is made, a "linker" collects all of the object files and writes the actual program. This way, if you change one source file, only that file needs to be recompiled and then the application needs to be re-linked.Processor Directives.
Syntax Checking.
This step ensures that the code is valid and will sequence into an executable program. Under some compilers, you may get messages or warnings indicating potential issues with your program.When an error is detected in the program, the compiler will normally report the filename and line that is giving trouble.
Object Code.
The compiler produces a machine code equivalent of the source code that can then be linked into the final program. The code itself can't be executed yet, as it has to complete the linking stage.
It's important to note after discussing the basics that compilation is a "one way street". That is, compiling a C source file into machine code is easy, but "decompiling" is not. Decompilers for C do exist, but they rarely create useful code.
Linking.
Linking combines the separate object codes into one complete program by integrating libraries and the code and producing either an executable program or to Library.
Automation.
many programmers choose to automate compilation, both in order to reduce user interaction requirements and to speed up the process by only recompiling modified files.