{"id":2311,"date":"2024-11-06T10:40:50","date_gmt":"2024-11-06T10:40:50","guid":{"rendered":"https:\/\/itnotes.apjsoftwares.in\/?p=2311"},"modified":"2024-11-06T10:40:50","modified_gmt":"2024-11-06T10:40:50","slug":"what-goes-inside-the-compilation-process","status":"publish","type":"post","link":"https:\/\/itnotes.apjsoftwares.in\/index.php\/2024\/11\/06\/what-goes-inside-the-compilation-process\/","title":{"rendered":"What goes inside the compilation process?"},"content":{"rendered":"\n<p>A compiler converts a C program into an executable. There are four phases for a C program to become an executable:&nbsp;<\/p>\n\n\n\n<ol>\n<li><strong>Pre-processing<\/strong><\/li>\n\n\n\n<li><strong>Compilation<\/strong><\/li>\n\n\n\n<li><strong>Assembly<\/strong><\/li>\n\n\n\n<li><strong>Linking<\/strong><\/li>\n<\/ol>\n\n\n\n<p>By executing the below command, we get all intermediate files in the current directory along with the executable.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong> $gcc -Wall -save-temps filename.c \u2013o filename<\/strong> <\/pre>\n\n\n\n<p>The following screenshot shows all generated intermediate files.<\/p>\n\n\n\n<p><img decoding=\"async\" loading=\"lazy\" alt=\"intermediate files in c compilation\" height=\"inherit\" src=\"https:\/\/media.geeksforgeeks.org\/wp-content\/uploads\/20230406112742\/c-compilation-intermediate-files.webp\" width=\"inherit\"><\/p>\n\n\n\n<p>Intermediate Files<\/p>\n\n\n\n<p>Let us one by one see what these intermediate files contain.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Pre-processing<\/h2>\n\n\n\n<p>This is the first phase through which source code is passed. This phase includes:<\/p>\n\n\n\n<ul>\n<li>Removal of Comments<\/li>\n\n\n\n<li>Expansion of Macros<\/li>\n\n\n\n<li>Expansion of the included files.<\/li>\n\n\n\n<li>Conditional compilation<\/li>\n<\/ul>\n\n\n\n<p>The preprocessed output is stored in the&nbsp;<strong>filename.i<\/strong>. Let\u2019s see what\u2019s inside filename.i: using&nbsp;<strong>$vi filename.i<\/strong>&nbsp;<\/p>\n\n\n\n<p>In the above output, the source file is filled with lots and lots of info, but in the end, our code is preserved.&nbsp;<\/p>\n\n\n\n<ul>\n<li>printf contains now a + b rather than add(a, b) that\u2019s because macros have expanded.<\/li>\n\n\n\n<li>Comments are stripped off.<\/li>\n\n\n\n<li><strong>#include&lt;stdio.h&gt;<\/strong>&nbsp;is missing instead we see lots of code. So header files have been expanded and included in our source file.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. Compiling<\/h2>\n\n\n\n<p>The next step is to compile filename.i and produce an; intermediate compiled output file&nbsp;<strong>filename.s<\/strong>. This file is in assembly-level instructions. Let\u2019s see through this file using<strong>&nbsp;$nano filename.s&nbsp;<\/strong>&nbsp;terminal command.<\/p>\n\n\n\n<p><img decoding=\"async\" loading=\"lazy\" alt=\"assembly code from C compiler\" height=\"inherit\" src=\"https:\/\/media.geeksforgeeks.org\/wp-content\/uploads\/20230406112833\/c-compilation-assembly-code.webp\" width=\"inherit\"><\/p>\n\n\n\n<p>Assembly Code File<\/p>\n\n\n\n<p>The snapshot shows that it is in assembly language, which the assembler can understand.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Assembling<\/h2>\n\n\n\n<p>In this phase the filename.s is taken as input and turned into&nbsp;<strong>filename.o<\/strong>&nbsp;by the assembler. This file contains machine-level instructions. At this phase, only existing code is converted into machine language, and the function calls like printf() are not resolved. Let\u2019s view this file using&nbsp;<strong>$vi filename.o<\/strong>&nbsp;<\/p>\n\n\n\n<p><img decoding=\"async\" loading=\"lazy\" alt=\"generated binary code in c compilation\" height=\"inherit\" src=\"https:\/\/media.geeksforgeeks.org\/wp-content\/uploads\/20230406112945\/c-compilation-binary-file_1.webp\" width=\"inherit\"><\/p>\n\n\n\n<p>Binary Code<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Linking<\/h2>\n\n\n\n<p>This is the final phase in which all the linking of function calls with their definitions is done. Linker knows where all these functions are implemented. Linker does some extra work also, it adds some extra code to our program which is required when the program starts and ends. For example, there is a code that is required for setting up the environment like passing command line arguments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A compiler converts a C program into an executable. There are four phases for a&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[],"_links":{"self":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/2311"}],"collection":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/comments?post=2311"}],"version-history":[{"count":1,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/2311\/revisions"}],"predecessor-version":[{"id":2312,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/2311\/revisions\/2312"}],"wp:attachment":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/media?parent=2311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/categories?post=2311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/tags?post=2311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}