<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tsetseg Yondongombo on Medium]]></title>
        <description><![CDATA[Stories by Tsetseg Yondongombo on Medium]]></description>
        <link>https://medium.com/@tsetseg.yo?source=rss-cdee0070f9de------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*uqgvptPqCfc2v5jEucpplw.jpeg</url>
            <title>Stories by Tsetseg Yondongombo on Medium</title>
            <link>https://medium.com/@tsetseg.yo?source=rss-cdee0070f9de------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 20:51:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tsetseg.yo/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[What is a C static library?]]></title>
            <link>https://medium.com/@tsetseg.yo/what-is-a-c-static-library-855240fd608d?source=rss-cdee0070f9de------2</link>
            <guid isPermaLink="false">https://medium.com/p/855240fd608d</guid>
            <dc:creator><![CDATA[Tsetseg Yondongombo]]></dc:creator>
            <pubDate>Sun, 11 Oct 2020 21:56:23 GMT</pubDate>
            <atom:updated>2020-10-13T07:19:26.804Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Static and Dynamic libraries in C</strong></h3><p>On creating and using libraries in C programming language</p><h4>What is a library and what is the purpose of it?</h4><p>A library is a collection of pre-compiled functions so that you can reuse them or share with other programmers. If you are using the C programming language, you may not realise but you use libraries all the time. Most commonly used library is the <strong><em>C standard library</em></strong> or <strong><em>lib C</em></strong> which includes <strong><em>printf()</em></strong>, <strong><em>gets()</em></strong>, <strong><em>strcpy()</em></strong> and so on. According to Wikipedia:</p><blockquote><em>The C standard library provides </em><a href="https://en.wikipedia.org/wiki/Macro_(computer_science)"><em>macros</em></a><em>, </em><a href="https://en.wikipedia.org/wiki/Data_type"><em>type</em></a><em> definitions and functions for tasks such as </em><a href="https://en.wikipedia.org/wiki/Character_string_(computer_science)"><em>string</em></a><em> handling, mathematical computations, input/output processing, </em><a href="https://en.wikipedia.org/wiki/Memory_management"><em>memory management</em></a><em>, and several other </em><a href="https://en.wikipedia.org/wiki/Operating_system"><em>operating system</em></a><em> services.</em></blockquote><p>As seen in the description of the<strong><em> lib C</em></strong>, we can group the functions that we are using repeatedly and place them in a library. We will then have fewer files to look for, and the computer will spend less time linking files. In fact, linking (see <a href="https://medium.com/@battsetsegyondongombo/the-compilation-process-in-c-3e7a9c368704">compilation process</a>) a program whose object files are stored and indexed in libraries is faster than linking a program whose object files are separate on the disk.</p><p>Libraries have mainly two categories; static and dynamic. By convention, library filename starts with <strong><em>lib.</em></strong> For Linux, any library that has the <strong><em>.a</em></strong> extension is a static library and the one with the .<strong><em>so </em></strong>extension the dynamic library (or shared library).</p><h3>How do libraries work?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/816/1*qVZx93rV64FI02IoF5H2vQ.png" /><figcaption>Courtesy to Holberton School</figcaption></figure><p>Let’s say we have a program <strong><em>0-main.c</em></strong> that calls a function <strong><em>_isupper()</em></strong> that checks whether a character is uppercase or not. This function belongs to the library <strong><em>libholberton.a</em></strong> which means we are using a function from another library.</p><p>When we compile the program, linker checks each file that is being compiled together. If the file is an object file, it will be placed into an executable file (i.e. which will now contain only binary codes). If it is a library, then the linker will check whether any symbol is being used. If yes, then the whole pre-compiled _isupper() file will be added to the executable file. This is called <strong><em>static linking.</em></strong></p><p>It is worthy to note that the linking priority goes to the dynamic library. In other words, linker looks for a file named <strong><em>libholberton.a </em></strong>only<strong><em> </em></strong>if there is no dynamic library named <strong><em>libholberton.so</em></strong>.</p><p>A disadvantage of static linking is that the size of the executable file increases with the number of functions that are being used. Another drawback of static linking is that when the library itself is modified or updated, the executable file will not be updated. To update the executable object file we will have to compile again with the newly updated library. Each time the library is updated, we need to recompile it every single time which makes it use inconvenient.</p><p><strong><em>Dynamic linking</em></strong> can resolve these issues. The function <strong><em>_isupper()</em></strong> in the library is already stored in a memory. Through dynamic linking, the linker will take only the address of the function <strong><em>_isupper() </em></strong>and add it to the executable file. Here, we will not be bothered by the size issue because our executable file will not include the whole pre-compiled function code but only the address of it. Another advantage is that if the library is updated, the address will point to the new library that is loaded in the memory so that we can avoid re-compiling the program.</p><h3>How to create libraries</h3><p>To create static libraries we can use a command called <strong><em>ar</em></strong> for <strong><em>archiver</em></strong>. ( The format of the static library is an archive). In addition to creating static libraries, <strong><em>ar</em></strong> is used to list the symbols stored in a static library, modify object files in the static library and so on. Let’s tasks our <strong><em>libholberton.a </em></strong>as an example and see how we created this library.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/948/1*WJ65qfgdgH-Wde4ZqqEhNg.png" /><figcaption>libholberton.a library containing _isupper() function</figcaption></figure><p>First, we will take or create a <strong><em>.c</em></strong> file(s) that contains the function(s). We will also create a header file that contains the function’s prototype.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/1*FKLZXSzht_Ic2puWCya1gw.png" /><figcaption>Header holberton.h file containing all prototypes</figcaption></figure><p>Next, we will compile function files to generate its object files.</p><blockquote><strong><em>gcc -Wall -pedantic -Werror -Wextra -c *.c</em></strong></blockquote><p>In our case, <strong><em>0-isupper.c</em></strong> will have <strong><em>0-isupper.o</em></strong> object file. Next, we will use the following command to create a static library. This command creates a static library named <strong><em>libholberton.a </em></strong>and<strong><em> </em></strong>puts copies of all objects files (including 0-isupper.o) in it. If the library file already exists, it has object file added to it or replaced if it is the newer version of it.</p><blockquote><strong><em>ar rc libholberton.a *.o</em></strong></blockquote><p>The <strong><em>c</em></strong> flag tells ar to create the library if it doesn’t already exist. The <strong><em>r</em></strong> flag tells it to replace older object files in the library, with the new object files.</p><p>Indexing the archive <strong><em>libholberton.a </em></strong>will help the compiler to speed up symbol searching inside the library. We can index the library with <strong><em>ranlib </em></strong>command.</p><blockquote><strong><em>ranlib libholberton.a</em></strong></blockquote><p>To list the symbols stored in a static library we can use <strong><em>nm</em></strong> command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*qt4LXv0h-Rk887OCj4h4YQ.png" /></figure><h3>How to use libraries</h3><p>To use our static library, we will add the library’s name ( excluding lib prefix and .a extension) to the list of object file names given to the linker.</p><blockquote><strong><em>gcc main.c -L. -lholberton -o result</em></strong></blockquote><p>This will create a program using file “main.c”, and any symbols it requires from the <strong><em>holberton</em></strong> static library to create an executable file named <strong><em>result</em></strong>. The <strong><em>-L</em></strong> flag — this flag tells the linker that libraries might be found in the given directory ( <strong><em>.</em></strong> referring to the current directory), in addition to the standard locations where the compiler looks for system libraries.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=855240fd608d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The compilation process in C]]></title>
            <link>https://medium.com/@tsetseg.yo/the-compilation-process-in-c-3e7a9c368704?source=rss-cdee0070f9de------2</link>
            <guid isPermaLink="false">https://medium.com/p/3e7a9c368704</guid>
            <dc:creator><![CDATA[Tsetseg Yondongombo]]></dc:creator>
            <pubDate>Wed, 16 Sep 2020 21:54:09 GMT</pubDate>
            <atom:updated>2020-09-16T22:22:33.613Z</atom:updated>
            <content:encoded><![CDATA[<p>When you are first introduced to a programming language, you can easily get lost in the forest of all the technical words. One of those tricky words for me while learning the language C was “compilation”. What does it even mean? According to the Cambridge dictionary, a compilation (noun) means the act of collecting information from a variety of places to arrange it in a book, report, or list.</p><p>In a programming language context, this is a multi-step process that transforms your keyboard-typed commands into a binary code which computer can understand and then execute. Some computer languages are interpreted directly which means your commands are immediately executed. Contrariwise, C is a compiled language which means it has to follow the previously mentioned compilation process.</p><p>Let me try to explain this process by using the command <strong><em>gcc main.c</em></strong> as an example.</p><blockquote><strong>GCC</strong> stands for “<strong>GNU</strong> Compiler Collection”. <strong>GCC</strong> is an integrated distribution of compilers for several major programming languages. These languages currently include <strong>C</strong>, <strong>C</strong>++, Objective-<strong>C</strong>, Objective-<strong>C</strong>++, Fortran, Ada, D, Go, and BRIG (HSAIL). The abbreviation <strong>GCC</strong> has multiple meanings in common use.</blockquote><h3>What happens when you type gcc main.c</h3><p><strong><em>main.c </em></strong>is the file name “main” with .<strong><em>c</em></strong> extension. Why do we need <strong><em>.c</em></strong>?</p><p>You will encounter these four kinds of files during the compilation process in C.</p><ol><li><strong>Source code</strong> files: contains function definitions, and have .c extension.</li><li><strong>Header</strong> files: contains C functions declarations and macro definitions for C built-in library functions, and have .h extension.</li><li><strong>Object</strong> files: contains functions definitions in binary form as a result of the compilation phase but not executable by themselves, and have .o extension.</li><li><strong>Binary executables</strong>: output from the last step of the compilation process.</li></ol><h3>The compilation process can be divided into four steps</h3><p>Below screenshot shows the content of our <strong><em>main.c</em></strong> file. It is a simple C program that prints “Hello C” message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_TkipMJMFLkyJM3CK-vTbQ.png" /><figcaption>main.c file</figcaption></figure><p>Now let’s see what happens when we compile or make the content of the file machine-readable by going through each of preprocessor, compiler, assembler and linker steps.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*i38MSoYXaWwekvHTzNOAXQ.png" /><figcaption>Courtesy : javatpoint.com</figcaption></figure><h4>The preprocessor</h4><pre>$ gcc -E main.c</pre><p>Before the compilation process begins, the <strong><em>main.c</em></strong> file is processed by the preprocessor. The preprocessor will take the source code as an input to produce another source code that is used by the compiler. A few different translation steps are going on here:</p><ul><li>It takes the preprocessor director ( first line starting with <strong><em>#</em></strong>) and interprets it. In our example, it is <strong><em>#include &lt;stdio.h&gt;</em></strong>, means the preprocessor will go to stdio.h file to look for declarations on printf() function.</li><li>Character replacement, replacement of comments with whitespace, and line slicing</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PzTCgZH1whHZfF9fqUZ3LQ.png" /><figcaption>Output from preprocessor</figcaption></figure><h4>The Compiler</h4><pre>$ gcc -c main.c</pre><p>At this stage, the compiler will take the preprocessed C source code into assembly code. Notice, after running the compiler command, we have a new file with .o extension, which contains a binary version of the source code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8Id1F8wAlAxL0fJ8I6fUbw.png" /><figcaption>main.o binary file</figcaption></figure><h4>The Assembler</h4><p>The object file is generated by the assembler by converting the assembly code into object code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mkAVFiCqysuwoTjFvzDK5Q.png" /></figure><p>Let’s see what’s in this file. We see that our code has become less human-readable and turned into something more understood by a machine. But it is not executable yet.</p><h4>The Linker</h4><p>In a final step of the compilation process, the linker will link together several object files, such as the ones created by the compiler and the one that has been collected from the library, into a binary executable. The linker will collect them and link them automatically.</p><p>Luckily we don’t need to go through this complicated process every single time. We can simply run <strong><em>gcc</em></strong> command and the computer will compile the file immediately for us provided that there is no error in our code.</p><pre><strong>gcc main.c </strong> will by default output a file named <strong>a.out</strong></pre><p>As illustrated in the example, when our original main.c file goes through the compilation process, the compiler will create a file with .out extension which is compiled executable file <strong><em>a.out</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-fwxWRQszJVNrsqEigXl0Q.png" /></figure><p>When we execute the .out file, we can see that our print() function to display the “Hello C” message has worked correctly.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3e7a9c368704" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What happens when you type ls *.c and hit Enter in your shell?]]></title>
            <link>https://medium.com/@tsetseg.yo/what-happens-when-you-type-ls-c-and-hit-enter-in-your-shell-b8e77ef869b6?source=rss-cdee0070f9de------2</link>
            <guid isPermaLink="false">https://medium.com/p/b8e77ef869b6</guid>
            <dc:creator><![CDATA[Tsetseg Yondongombo]]></dc:creator>
            <pubDate>Mon, 14 Sep 2020 21:40:47 GMT</pubDate>
            <atom:updated>2020-09-14T21:40:47.296Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R7l_rUm7A_923L3YcGDRsw.jpeg" /><figcaption>Photo by Gerald Croes</figcaption></figure><p><strong>What is Shell?</strong></p><p>Shell is a user interface that takes the user’s commands, interprets them before providing them to the operating system to execute. There are two types of operating system shells:</p><ol><li>Graphical user interface (GUI) — where users manipulate programs graphically via desktop, start menu, taskbar etc. It allows operations such as opening, closing, copying simply by dragging and clicking your mouse.</li><li>Command-line interface (CLI) — it uses keyboard-typed command lines to give instructions to the operating system to carry out operations such as logging in a user, listing files, creating files and many others. You can access shell by opening your terminal window.</li></ol><p>Let’s take a closer look at the CLI and see what happens when we type a command in the shell.</p><p>When the terminal is launched, we can see the shell prompt displays your user name followed by the dollar ($) sign prompt. This means that the shell is ready to accept your commands as typed in from your keyboard. Shell will not process commands until you hit &lt;Enter&gt; which indicates the end of a line.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/920/1*a2k_GpUqmn1F4XgIPhHYIg.png" /></figure><p><strong><em>ls</em> command</strong></p><p>The file system is part of the operating system that manages files and directories. <strong><em>ls</em></strong> is a shell command that lists files and directories within the file system. In other words, it allows you to see the contents of a directory.</p><p>Let’s type <strong><em>ls</em></strong> and press &lt;Enter&gt;. First, the shell will split your command into command, variable, or other symbols, which we call “tokenizing”, to make it recognizable by it. Once these commands, variable and other symbols are recognized, commands may then be executed and the output of <strong><em>ls</em></strong> command is displayed.</p><p>The example below shows a working directory called MyLibrary contains these 4 files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*qPABZldN_AM47-JpNKq02Q.png" /></figure><p>If you want to know more about the directory contents, you can use options according to your need. ls command should respect the following syntax:</p><blockquote>ls [OPTIONS] [FILES]</blockquote><p>Let’s type <strong><em>ls </em></strong>command with long format option (-l) to display more information for each item in the MyLibrary directory such as file permissions, file owner and group, file size, date and time, file name information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bryZp6IweWRwWsqStN06rA.png" /><figcaption>ls -l MyLibrary</figcaption></figure><p>We can also use special characters called <a href="https://tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm"><strong>wildcards</strong></a> to make our command more efficient. The example below shows that using wild card <strong><em>*.c</em></strong> with the <strong><em>ls</em></strong> ll display the files ending with characters “.c”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HN9DqrCZdBIid1_QODIOOg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b8e77ef869b6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>