<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bharathy</title>
    <description>The latest articles on DEV Community by Bharathy (@abharathy).</description>
    <link>https://dev.to/abharathy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F397078%2F01c769ae-9731-4685-9f02-8c7dff7cd4e0.jpeg</url>
      <title>DEV Community: Bharathy</title>
      <link>https://dev.to/abharathy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abharathy"/>
    <language>en</language>
    <item>
      <title>Journey of a .c file</title>
      <dc:creator>Bharathy</dc:creator>
      <pubDate>Sat, 30 May 2020 11:41:39 +0000</pubDate>
      <link>https://dev.to/abharathy/journery-of-a-c-file-pde</link>
      <guid>https://dev.to/abharathy/journery-of-a-c-file-pde</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;em&gt;TL;DR&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;In order to generate a &lt;code&gt;.exe&lt;/code&gt; file from a &lt;code&gt;.c&lt;/code&gt; file, there are some temporary intermediate files (.i, .s, .o) that are created at each phase of the compilation process&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;



&lt;p&gt;Having taught a lot of students C programming, I felt that the knowledge and understanding of what is happening behind the scenes or on execution of the commands will help learners with better understanding. So during this COVID-19 pandemic lock down I thought of sharing what I know to new learners of programming (those who are depending on online sources)&lt;/p&gt;

&lt;p&gt;Most of the beginners of "learning C"  know that on compilation, a &lt;code&gt;.exe&lt;/code&gt; file is created/generated. But have you ever wondered how it happens? This post is about the step by step transformation of a &lt;code&gt;.c&lt;/code&gt; file to a &lt;code&gt;.exe&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Let us first write a simple C file called &lt;code&gt;welcome.c&lt;/code&gt; that simply prints a welcome message in the console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Welcome to learning C"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I use GCC (GNU Compiler Collection) version 9.2.0. Instead of using an IDE, using a code editor  and running the commands in the command line will aid in enhancing your understanding of what happens under the hood.&lt;/p&gt;

&lt;p&gt;In the command line, run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc welcome.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will generate an &lt;code&gt;a.exe&lt;/code&gt; file by default. &lt;br&gt;
To get the output file with the desired name &lt;code&gt;-o&lt;/code&gt; option can be used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc  welcome.c &lt;span class="nt"&gt;-o&lt;/span&gt; welcome
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On execution of the above command, the output file &lt;code&gt;welcome.exe&lt;/code&gt; is generated. However during this conversion, three other intermediate temporary files(.i, .s and .o) are generated and deleted.&lt;/p&gt;

&lt;p&gt;Conversion of a .c to a .exe has the following phases.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Preprocessing &lt;/li&gt;
&lt;li&gt;Compiling&lt;/li&gt;
&lt;li&gt;Assembling&lt;/li&gt;
&lt;li&gt;Linking &lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Preprocessing
&lt;/h4&gt;

&lt;p&gt; This is the first phase of the conversion. The file named &lt;code&gt;cpp.exe&lt;/code&gt; is the GNU's preprocessor. &lt;code&gt;cpp.exe&lt;/code&gt; can be used to process the &lt;code&gt;.c&lt;/code&gt; file before compilation. This &lt;code&gt;cpp.exe&lt;/code&gt; file can be seen in the bin folder.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frzodmbii9b31hd62tnyn.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frzodmbii9b31hd62tnyn.PNG" alt="Preprocessor Assembler and Compiler in bin folder"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cpp welcome.c &lt;span class="nt"&gt;-o&lt;/span&gt; welcome.i
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;generates the preprocessed file &lt;code&gt;welcome.i&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-o&lt;/code&gt; is used to mention the output file. Here the output of preprocessing phase is &lt;code&gt;welcome.i&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;.i file is the first intermediate  file&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;.i&lt;/code&gt; file can also be generated by &lt;code&gt;-E&lt;/code&gt; option with gcc in any one format as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc &lt;span class="nt"&gt;-E&lt;/span&gt; welcome.c &lt;span class="nt"&gt;-o&lt;/span&gt; welcome.i   
gcc &lt;span class="nt"&gt;-E&lt;/span&gt; welcome.c &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; welcome.i
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us go through the &lt;code&gt;welcome.c&lt;/code&gt; file now.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;stdio.h&lt;/code&gt; in line 1 of &lt;code&gt;welcome.c&lt;/code&gt; is a file that contains function declaration/function prototype for all i/o functions. Since C is a forward declaration language, it is mandatory to inform the compiler about the existence of an entity before the entity is being used. (e.g., declaring a function before being called). &lt;br&gt;
&lt;code&gt;welcome.c&lt;/code&gt; file uses &lt;code&gt;printf()&lt;/code&gt; to display "Welcome to learning C". The code for &lt;code&gt;printf()&lt;/code&gt; is available as a library file(pre-compiled error free content). But it needs to mentioned to the compiler, at the time of compiling &lt;code&gt;welcome.c&lt;/code&gt;. &lt;br&gt;
By including &lt;code&gt;#include&amp;lt;stdio.h&amp;gt;&lt;/code&gt;, the entire file contents are read and embedded in the source code. Instead of copying the entire file contents, function declaration of printf() alone can be mentioned as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kr"&gt;restrict&lt;/span&gt; &lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...);&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Welcome to learning C"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replacing header file(s) is one of the activities in the preprocessing phase. This phase also includes removal of comments, removal of extra spaces, etc., There are plenty of material available on preprocessing.&lt;/p&gt;

&lt;p&gt;In short, the preprocessing phase retains only the necessary statements to compile and it generates  a &lt;code&gt;.i&lt;/code&gt; file as output. &lt;strong&gt;Compilation  errors are not identified in the preprocessor phase.&lt;/strong&gt; The above code has a missing semicolon (syntax error) at line no.5. However, the file &lt;code&gt;welcome.i&lt;/code&gt; provides no indication of this. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fked53v4uvswx4c53tdkm.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fked53v4uvswx4c53tdkm.PNG" alt="welcome.i - Preprocessing is completed. Missing ; at line no. 9 not detected"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Compiling
&lt;/h4&gt;

&lt;p&gt;In this phase the syntax errors are identified.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc  &lt;span class="nt"&gt;-S&lt;/span&gt; welcome.i
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-S&lt;/code&gt; option in the above command tells the compiler to generate the equivalent assembly code.The missing semicolon is caught in a compilation error as follows.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhkgagi108ntfnel7dcy6.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhkgagi108ntfnel7dcy6.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On successful compilation, processor dependent assembly code is generated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgh9qlc144jg7jmuotrpz.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgh9qlc144jg7jmuotrpz.PNG" alt="welcome.s - Assembly equivalent for welcome.i"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.s file is the second intermediate file.&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Assembling
&lt;/h4&gt;

&lt;p&gt; The &lt;code&gt;.s&lt;/code&gt; file is taken as input by the GNU's assembler &lt;code&gt;as.exe&lt;/code&gt; and it generates the file with the &lt;code&gt;.o&lt;/code&gt; extension. The .o file is known as object file.The Object file is in machine code and hence cannot be read by code editors or ĪDEs. The function calls to the library functions (&lt;code&gt;printf()&lt;/code&gt; in this example) still remain unresolved. These function calls are yet to be found from the libraries and attached/linked  to the object code. So, the &lt;code&gt;.o&lt;/code&gt; files are not in an executable form.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwitl5ddlwsa8ol7sh16z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwitl5ddlwsa8ol7sh16z.png" alt=".c to .o"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.o is the third intermediate file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The .c file can also be generated by&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc  &lt;span class="nt"&gt;-c&lt;/span&gt;  &lt;span class="nt"&gt;-o&lt;/span&gt; welcome.o welcome.i
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Linking
&lt;/h4&gt;

&lt;p&gt;gcc uses a file named &lt;code&gt;ld.exe&lt;/code&gt; as the linker for linking. &lt;/p&gt;

&lt;p&gt;An overview of the the linker's responsibility&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;takes one or more object files as input &lt;/li&gt;
&lt;li&gt;combines object files to resolve unresolved symbols. &lt;/li&gt;
&lt;li&gt;generates error(linker error) messages, on finding duplicate symbols(e.g. declaring the same variable in more than one object file in global scope)&lt;/li&gt;
&lt;li&gt;if some symbols (eg. malloc ) are unresolved, the linker checks the libraries given to it in the mentioned order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At the end of the linking phase,all the different parts of code are merged, symbols are resolved and an executable code (&lt;code&gt;.exe&lt;/code&gt; file) is generated. However, memory address are yet to be assigned for the code and data.&lt;/p&gt;

&lt;p&gt;For beginners, while building small programs, linking is not an issue. Hence, I am not dwelling too much into these details.&lt;/p&gt;

&lt;p&gt;All three intermediate files and an executable file can also be obtained by the following options in gcc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc &lt;span class="nt"&gt;-o&lt;/span&gt; welcome.exe &lt;span class="nt"&gt;-save-temps&lt;/span&gt; welcome.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>c</category>
      <category>gcc</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
