Compile That C File
So I’ve been learning terminal, Shell, Linux — all pretty foreign to me until last week ago. Today I officially stepped into C world, and I learned something never raised a question on me before — why do you need to compile a C file before running it? What does it do?
It turned out some programming languages are interpreted in the environment they are running, like Shell. Some are complied, like C, through a complier before they can be executed.
For example, if you want to compile a main.c file in Linux, you can use gcc as compiler. First you type in “gcc main.c”, after you hit enter, a separate processor program will take the source file, expand the header file and generate a modified source and pass it to the compiler.
The complier will then take the c file and turn it into assembly code, so the assembler can read and convert it into object code — a machine readable binary format.
The last step of the compiling process is a linker will take these object files created by complier, add links to different libraries if they are used, and generate a final executable file.
So that’s what happens behind the scenes within just a few second, pretty fascinating, IMHO.