How does a compiler turns your source code into executable?


Suppose we write a C program as two files p1.c and p2.c. 

We can then compile this code on an IA32 machine using a Unix command line:
unix> gcc -O1 -o p p1.c p2.c

Your compiler actually invokes a sequence of programs to turn your source code into your executable.

1. C preprocess expands the source code to include any files specified with the #include command and to expand any macros specific with #define declarations. 

2. The compiler generates assembly- code versions of the two source files having names p1.s and p2.s

3.  The assembler converts the assembly code into binary object-code files p1.o and p2.o. Object code is one form of machine code—it contains binary representations of all of the instructions, but the addresses of global values are not yet filled in. 

4. Finally, the linker merges these two object – code files along with code implementing library functions(e.g., printf) and generates the final executable codefile p.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: