How do you compile C code? How do you make executable programs from C source code files? This tutorial outlines the basic C compilation model and processes that address those questions. You will have a better understanding of how to make executable file from C source code files.
The following picture illustrates the C compilation model.
Let’s examine each component in more detail.
- The input of the C compilation process is C source code file and its result is executable file. The output of previous component is the input of the next component.
- The preprocessor uses source code as input. The preprocessor is in charge of removing all the comments and interpreting preprocessor directives indicated by the hash
#sign. For example in the hello world program, the
#includedirective is used to include code of corresponding file e.g., standard I/O file,
stdio.h. The preprocessor will include the source code of in the
stdio.hfile in the main program before transferring it to the compiler.
- After preprocessor processing the source, it transfers the result to the C compiler. The C compiler is responsible for translating from plain source code into assembly code. We are using gcc compiler to translate C code into assembler code.
- The assembler is in charge of creating object code. On Windows systems the object code files has
.objextension and on UNIX systems the object code file has
- If in the source code file, you use functions from a library, the link editor will combine these functions with the
main()function to make an executable file of the program. The link editor sometimes is also known as linker.
The compilation process seems to be lengthy. It takes time and requires more efforts if you compile a program yourself. However with a good IDE like CodeBlocks, you can configure all the components once and the IDE will handle the tasks for you automatically with a push of button.
If you want to use a different C compiler to make C program works on a specific platform, you can check it out the Wikipedia, C compilers list page.