<?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: Valerio Chiodi</title>
    <description>The latest articles on DEV Community by Valerio Chiodi (@valeriochiodi).</description>
    <link>https://dev.to/valeriochiodi</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%2F737157%2Fbc90f00a-128a-49f5-9d53-a3e0f9f67473.png</url>
      <title>DEV Community: Valerio Chiodi</title>
      <link>https://dev.to/valeriochiodi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/valeriochiodi"/>
    <language>en</language>
    <item>
      <title>Build your own C library</title>
      <dc:creator>Valerio Chiodi</dc:creator>
      <pubDate>Wed, 27 Oct 2021 14:22:31 +0000</pubDate>
      <link>https://dev.to/valeriochiodi/build-your-own-c-library-16oo</link>
      <guid>https://dev.to/valeriochiodi/build-your-own-c-library-16oo</guid>
      <description>&lt;h2&gt;
  
  
  &lt;em&gt;Hi everyone, this is my first article.&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;If i make any grammar mistakes, let me know.&lt;br&gt;
I am not a native english speaker&lt;/p&gt;

&lt;p&gt;Today I want to talk to you about &lt;strong&gt;"personal C library"&lt;/strong&gt;.&lt;br&gt;
Often functions written by us are very useful and will likely need to be reused in new programs.&lt;br&gt;
You can then group these functions into a useful &lt;strong&gt;library&lt;/strong&gt; to facilitate their reuse.&lt;/p&gt;

&lt;p&gt;First let's start with a simple program that we all know. &lt;strong&gt;HELLO WORLD!&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;include&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;stdio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nx"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="cm"&gt;/*standard library */&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;HELLO WORLD!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="cm"&gt;/*output displayed*/&lt;/span&gt;
    &lt;span class="nx"&gt;getchar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="cm"&gt;/*Press any key to continue.*/&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I used &lt;code&gt;Visual Studio Code&lt;/code&gt; as a text editor, but you can use whatever you want.&lt;/p&gt;

&lt;p&gt;So far everything ok. &lt;br&gt;
But if I wanted to put my function in a personal library to be able to reuse it, how do I do it?&lt;/p&gt;

&lt;p&gt;Each library consists of two parts: a header file and the file that contains the code.&lt;br&gt;
The header file, which is denoted with a suffix &lt;strong&gt;.h&lt;/strong&gt; &lt;br&gt;
The header file contains constants and types, along with prototypes for the functions available in the library.&lt;/p&gt;
&lt;h3&gt;
  
  
  Header file
&lt;/h3&gt;

&lt;p&gt;First let's write our header file and save it in a file called &lt;code&gt;mylib.h&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* mylib.h */&lt;/span&gt;
&lt;span class="nx"&gt;extern&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nx"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line of code is a function prototype.&lt;br&gt;
&lt;em&gt;The word &lt;strong&gt;extern&lt;/strong&gt; in &lt;strong&gt;C&lt;/strong&gt;&lt;/em&gt; represents functions that will be linked at a later time&lt;/p&gt;

&lt;p&gt;Now let's write our code in a file called &lt;code&gt;mylib.c&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* mylib.c */&lt;/span&gt;
&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;include&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;stdio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;include&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mylib.h&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nx"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;HELLO WORLD! with my personal library &lt;/span&gt;&lt;span class="dl"&gt;"&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;Note that the file includes its own header file (&lt;code&gt;mylib.h&lt;/code&gt;) and &lt;code&gt;this is enclosed in double quotes&lt;/code&gt; instead of the major and minor symbols. &lt;br&gt;
The latter being used only for system libraries.&lt;/p&gt;
&lt;h3&gt;
  
  
  It's time to create the main program!
&lt;/h3&gt;

&lt;p&gt;Then write the following program into a file and call it &lt;code&gt;main.c&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* main.c */&lt;/span&gt;
&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;include&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;stdio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="nx"&gt;include&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mylib.h&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nx"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;getchar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nx"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;HELLO PROGRAMMER ! this greeting does not use your personal library &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;getchar&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;This code includes the utility (&lt;code&gt;mylib.h&lt;/code&gt;) library we created.&lt;/p&gt;

&lt;h2&gt;
  
  
  FILL IN AND RUN THE CODE WITH A LIBRARY
&lt;/h2&gt;

&lt;p&gt;If you have an &lt;strong&gt;IDE&lt;/strong&gt; to program in &lt;strong&gt;C&lt;/strong&gt;, you don't need to know how to compile, it will do it for you.&lt;/p&gt;

&lt;h4&gt;
  
  
  So, run the program and watch the magic.
&lt;/h4&gt;

&lt;p&gt;Otherwise if you want to create an executable, for example if you use &lt;strong&gt;LINUX&lt;/strong&gt; and you have a &lt;strong&gt;gcc compiler&lt;/strong&gt; installed, here are the steps you need to do.&lt;/p&gt;

&lt;p&gt;Open the terminal in the folder containing the three files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;mylib.h&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;mylib.c&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;main.c&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and type: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;gcc -c -g mylib.c&lt;/code&gt;&lt;br&gt;
&lt;code&gt;gcc -c -g main.c&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This line creates a file called &lt;code&gt;mylib.o&lt;/code&gt; and &lt;code&gt;main.o&lt;/code&gt; which contains the machine code for the main program.&lt;br&gt;
The &lt;strong&gt;-c&lt;/strong&gt; option causes the compiler to produce an object file for the library&lt;/p&gt;

&lt;h5&gt;
  
  
  Now we need to link the two object files by typing the following command.
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;gcc -o myprogramname main.o mylib.o&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command links main.o with mylib.o to form an executable called &lt;code&gt;myprogramname&lt;/code&gt;. To run the program, type &lt;code&gt;myprogramname&lt;/code&gt; on the command line.&lt;/p&gt;

&lt;p&gt;Or if you also use &lt;code&gt;Visual Studio Code&lt;/code&gt;, like me, an &lt;code&gt;.exe&lt;/code&gt; will be created in the folder have you used.&lt;/p&gt;

&lt;h5&gt;
  
  
  I hope I was clear and that you enjoyed this article !
&lt;/h5&gt;

&lt;h4&gt;
  
  
  Now its time for you to create your own libraries.
&lt;/h4&gt;

</description>
      <category>cpp</category>
      <category>visualcode</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
