{"id":410,"date":"2026-01-17T17:06:59","date_gmt":"2026-01-17T17:06:59","guid":{"rendered":"https:\/\/pipeawk.com\/?p=410"},"modified":"2026-01-24T19:47:41","modified_gmt":"2026-01-24T19:47:41","slug":"the-mistery-of-dynamically-linked-or-statically-linked-executables","status":"publish","type":"post","link":"https:\/\/pipeawk.com\/index.php\/2026\/01\/17\/the-mistery-of-dynamically-linked-or-statically-linked-executables\/","title":{"rendered":"The Mystery of Dynamically Linked or Statically Linked Executables"},"content":{"rendered":"<p>Hello readers, today I decided to explain a deep concept not usually discussed often because of the abstractions computer languages have created. But in the end, on a Linux\/UNIX system, there is an executable binary file that can be either dynamically or statically linked. I hope this post find as many users as possible due to the fact that I could not find an easy explanation out there and i decided to write my own to share for the benefit of people that want to learn Linux\/UNIX in general.<\/p>\r\n<p>Enough, let&#8217;s dig into the subject&#8230;<!--more--><\/p>\r\n<p>In order to understand the subject we need to use a low level language like C or C++. For simplicity we are using C. If you don&#8217;t know, C and C++ are heavily used in low level program development, like the Linux Kernel.<\/p>\r\n<p>Now, let&#8217;s first setup the environment to use a C compiler, which is required for this exercise. You are welcome to use a container for that, but for simplicity I will simply list the packages required on Ubuntu, but the same applies to all Linux distros all you have to do is install the C compiler the supported way.<\/p>\r\n<p>In\u00a0 Ubuntu you will simply run the two following commands:<\/p>\r\n<blockquote>\r\n<p>sudo apt update<br \/>sudo apt install gcc<\/p>\r\n<\/blockquote>\r\n<p>This will install gcc all the required dependencies. Now that you have the required software, we will create a directory called <strong>linking<\/strong> and cd into it with the following commands:<\/p>\r\n<blockquote>\r\n<p>mkdir ~\/linking<br \/>cd ~\/linking<\/p>\r\n<\/blockquote>\r\n<p>Now with your favorite editor create the following file named\u00a0<strong>linktest.c<\/strong> with the following content:<\/p>\r\n<blockquote>\r\n<p>#include &lt;stdio.h&gt;<\/p>\r\n<p>void main() {<br \/>printf(&#8220;Hello There\\n&#8221;);<br \/>}<\/p>\r\n<\/blockquote>\r\n<p>Save the file and issue the following command to compile\u00a0<strong>linktest:<\/strong><\/p>\r\n<blockquote>\r\n<p>gcc linktest.c -o linktest<\/p>\r\n<\/blockquote>\r\n<p>If all goes well, the compiler produces an executable; now try to execute it with the following command:<\/p>\r\n<blockquote>\r\n<p>.\/linktest<\/p>\r\n<\/blockquote>\r\n<p>You should see the output of\u00a0<strong>Hello There<\/strong>. If you reach this point everything is going according to plans, if you receive a different output of error, make sure all the steps listed above are completed without errors\/compilation errors. C is very picky about syntax, so make sure you put exactly what&#8217;s listed above.<\/p>\r\n<p>Now that we have an executable, type &#8220;ls -l&#8221; to see the properties of the file, on my system, the file is about 15k in size, relatively small. The executable, by default is compiled and dynamically linked to a set of system libraries. Let&#8217;s dig in details. Execute the following command to see all the dependent, dynamically linked libraries:<\/p>\r\n<blockquote>\r\n<p>ldd linktest<\/p>\r\n<\/blockquote>\r\n<p>Exept for the library versions, you should see an output similar to the following:<\/p>\r\n<blockquote>\r\n<p>linux-vdso.so.1 (0x00007fffc4bfe000)<br \/>libc.so.6 =&gt; \/lib\/x86_64-linux-gnu\/libc.so.6 (0x000072dd9707f000)<br \/>\/lib64\/ld-linux-x86-64.so.2 (0x000072dd972b2000)<\/p>\r\n<\/blockquote>\r\n<p>Now, that you&#8217;ve reached this point, let&#8217;s explain dynamic linking and what it actually does. Any program in Linux is dependent on the libc, which is the standard runtime. You see it listed as <strong>libc.so.6<\/strong>. But you also see other files listed, in this case, I see linux-vdso because of the virtual layer I am using and ld-linux-x86 for the final kernel link.<\/p>\r\n<p>When you execute the program, the kernel retrieves the necessary dependent executing calls to make the whole program run on the system.<\/p>\r\n<p>Now, in order to better explain dynamic linking, let&#8217;s create the same executable with static linking. By default the compiler executed two steps, a compilation step into a .o type object and a final link step to produce the executable. In order to make a static linked executable, we have to instruct the compiler to include all the dependencies into the executable. Let&#8217;s now statically compile the executable with the following command:<\/p>\r\n<blockquote>\r\n<p>gcc -static linktest.c -o static_linktest<\/p>\r\n<\/blockquote>\r\n<p>If everything goes well, you will create an executable called\u00a0<strong>static_linktest<\/strong> which will be bigger in size at the\u00a0<strong>linktest<\/strong> one. Now execute\u00a0<strong>ls -l<\/strong> to list the directory content, you should see an output similar to the following:<\/p>\r\n<blockquote>\r\n<p>-rwxr-xr-x 1 devuser devgroup 15960 Jan 17 11:46 linktest<br \/>-rw-r&#8211;r&#8211; 1 devuser devgroup 63 Jan 17 11:46 linktest.c<br \/>-rwxr-xr-x 1 devuser devgroup 900352 Jan 17 11:58 static_linktest<\/p>\r\n<\/blockquote>\r\n<p>Now, sizes will vary based on system versions, but one thing is clear, the static version is much bigger than the dynamic version. Let&#8217;s now execute the following command on the static executable:<\/p>\r\n<blockquote>\r\n<p>ldd static_linktest<\/p>\r\n<\/blockquote>\r\n<p>You should get a message that indicated that this is not a dynamic executable, which proves the point of the exercise.<\/p>\r\n<p>A statically linked executable contains all the dependencies inside and does not require the external libraries like the dynamically linked one. Basically a portion of\u00a0<strong>libc.6<\/strong>, <strong>ld-linux<\/strong>,\u00a0and\u00a0<strong>linux-vds0<\/strong> are included (statically linked) in the file.<\/p>\r\n<p>Now, the question should pop-up, why would I use one vs the other and when. For some of you, the answers will be self deduced, for the rest, the answers are coming in the next post&#8230;<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Hello readers, today I decided to explain a deep concept not usually discussed often because of the abstractions computer languages have created. But in the end, on a Linux\/UNIX system, there is an executable binary file that can be either dynamically or statically linked. I hope this post find as many users as possible due &hellip; <a href=\"https:\/\/pipeawk.com\/index.php\/2026\/01\/17\/the-mistery-of-dynamically-linked-or-statically-linked-executables\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;The Mystery of Dynamically Linked or Statically Linked Executables&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-410","post","type-post","status-publish","format-standard","hentry","category-unix"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/posts\/410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/comments?post=410"}],"version-history":[{"count":9,"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/posts\/410\/revisions"}],"predecessor-version":[{"id":420,"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/posts\/410\/revisions\/420"}],"wp:attachment":[{"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/media?parent=410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/categories?post=410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pipeawk.com\/index.php\/wp-json\/wp\/v2\/tags?post=410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}