{"id":13197,"date":"2019-04-01T07:26:22","date_gmt":"2019-04-01T07:26:22","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13197"},"modified":"2019-04-01T07:26:22","modified_gmt":"2019-04-01T07:26:22","slug":"understanding-shared-libraries-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/understanding-shared-libraries-in-linux\/","title":{"rendered":"Understanding Shared Libraries in Linux"},"content":{"rendered":"<p>In programming, a library is an assortment of pre-compiled pieces of code that can be reused in a program. Libraries simplify life for programmers, in that they provide reusable functions, routines, classes, data structures and so on (written by a another programmer), which they can use in their programs.<\/p>\n<p>For instance, if you are building an application that needs to perform math operations, you don\u2019t have to create a new math function for that, you can simply use existing functions in libraries for that programming language.<\/p>\n<p>Examples of libraries in Linux include\u00a0<strong>libc<\/strong>\u00a0(the standard C library) or\u00a0<strong>glibc<\/strong>\u00a0(GNU version of the standard C library),\u00a0<strong>libcurl<\/strong>\u00a0(multiprotocol file transfer library),\u00a0<strong>libcrypt<\/strong>\u00a0(library used for encryption, hashing, and encoding in C) and many more.<\/p>\n<p>Linux supports two classes of libraries, namely:<\/p>\n<ul>\n<li><strong>Static libraries<\/strong>\u00a0\u2013 are bound to a program statically at compile time.<\/li>\n<li><strong>Dynamic or shared libraries<\/strong>\u00a0\u2013 are loaded when a program is launched and loaded into memory and binding occurs at run time.<\/li>\n<\/ul>\n<p>Dynamic or shared libraries can further be categorized into:<\/p>\n<ul>\n<li><strong>Dynamically linked libraries<\/strong>\u00a0\u2013 here a program is linked with the shared library and the kernel loads the library (in case it\u2019s not in memory) upon execution.<\/li>\n<li><strong>Dynamically loaded libraries<\/strong>\u00a0\u2013 the program takes full control by calling functions with the library.<\/li>\n<\/ul>\n<h4>Shared Library Naming Conventions<\/h4>\n<p>Shared libraries are named in two ways: the library name (a.k.a\u00a0<strong>soname<\/strong>) and a \u201cfilename\u201d (absolute path to file which stores library code).<\/p>\n<p>For example, the\u00a0<strong>soname<\/strong>\u00a0for\u00a0<strong>libc<\/strong>\u00a0is\u00a0<strong>libc.so.6<\/strong>: where\u00a0<strong>lib<\/strong>\u00a0is the prefix,\u00a0<strong>c<\/strong>\u00a0is a descriptive name, so means shared object, and\u00a0<strong>6<\/strong>\u00a0is the version. And its filename is:\u00a0<strong>\/lib64\/libc.so.6<\/strong>. Note that the soname is actually a symbolic link to the filename.<\/p>\n<h4>Locating Shared Libraries in Linux<\/h4>\n<p>Shared libraries are loaded by\u00a0<strong>ld.so<\/strong>\u00a0(or\u00a0<strong>ld.so.x<\/strong>) and\u00a0<strong>ld-linux.so<\/strong>\u00a0(or\u00a0<strong>ld-linux.so.x<\/strong>) programs, where\u00a0<strong>x<\/strong>\u00a0is the version. In Linux,\u00a0<strong>\/lib\/ld-linux.so.x<\/strong>\u00a0searches and loads all shared libraries used by a program.<\/p>\n<p>A program can call a library using its library name or filename, and a library path stores directories where libraries can be found in the filesystem. By default, libraries are located in\u00a0<strong>\/usr\/local\/lib<\/strong>,\u00a0<strong>\/usr\/local\/lib64<\/strong>,\u00a0<strong>\/usr\/lib<\/strong>\u00a0and\u00a0<strong>\/usr\/lib64<\/strong>; system startup libraries are in\u00a0<strong>\/lib<\/strong>\u00a0and\u00a0<strong>\/lib64<\/strong>. Programmers can, however, install libraries in custom locations.<\/p>\n<p>The library path can be defined in\u00a0<strong>\/etc\/ld.so.conf<\/strong>\u00a0file which you can edit with a command line editor.<\/p>\n<pre># vi \/etc\/ld.so.conf \r\n<\/pre>\n<p>The line(s) in this file instruct the kernel to load file in\u00a0<strong>\/etc\/ld.so.conf.d<\/strong>. This way, package maintainers or programmers can add their custom library directories to the search list.<\/p>\n<p>If you look into the\u00a0<strong>\/etc\/ld.so.conf.d<\/strong>\u00a0directory, you\u2019ll see\u00a0<strong>.conf<\/strong>\u00a0files for some common packages (kernel, mysql and postgresql in this case):<\/p>\n<pre><strong># ls \/etc\/ld.so.conf.d<\/strong>\r\n\r\nkernel-2.6.32-358.18.1.el6.x86_64.conf  kernel-2.6.32-696.1.1.el6.x86_64.conf  mariadb-x86_64.conf\r\nkernel-2.6.32-642.6.2.el6.x86_64.conf   kernel-2.6.32-696.6.3.el6.x86_64.conf  postgresql-pgdg-libs.conf\r\n<\/pre>\n<p>If you take a look at the mariadb-x86_64.conf, you will see an absolute path to package\u2019s libraries.<\/p>\n<pre><strong># cat mariadb-x86_64.conf<\/strong>\r\n\r\n\/usr\/lib64\/mysql\r\n<\/pre>\n<p>The method above sets the library path permanently. To set it temporarily, use the\u00a0<strong>LD_LIBRARY_PATH<\/strong>environment variable on the command line. If you want to keep the changes permanent, then add this line in the shell initialization file\u00a0<strong>\/etc\/profile<\/strong>\u00a0(global) or\u00a0<strong>~\/.profile<\/strong>\u00a0(user specific).<\/p>\n<pre># export LD_LIBRARY_PATH=\/path\/to\/library\/file\r\n<\/pre>\n<h4>Managing Shared Libraries in Linux<\/h4>\n<p>Let us now look at how to deal with shared libraries. To get a list of all shared library dependencies for a binary file, you can use the\u00a0<strong>ldd utility<\/strong>. The output of\u00a0<strong>ldd<\/strong>\u00a0is in the form:<\/p>\n<pre>library name =&gt;  filename (some hexadecimal value)\r\nOR\r\nfilename (some hexadecimal value)  #this is shown when library name can\u2019t be read\r\n<\/pre>\n<p>This command shows all shared library dependencies for the\u00a0<a href=\"https:\/\/www.tecmint.com\/tag\/linux-ls-command\/\" target=\"_blank\" rel=\"noopener\">ls command<\/a>.<\/p>\n<pre># ldd \/usr\/bin\/ls\r\nOR\r\n# ldd \/bin\/ls\r\n<\/pre>\n<h5>Sample Output<\/h5>\n<pre>\tlinux-vdso.so.1 =&gt;  (0x00007ffebf9c2000)\r\n\tlibselinux.so.1 =&gt; \/lib64\/libselinux.so.1 (0x0000003b71e00000)\r\n\tlibrt.so.1 =&gt; \/lib64\/librt.so.1 (0x0000003b71600000)\r\n\tlibcap.so.2 =&gt; \/lib64\/libcap.so.2 (0x0000003b76a00000)\r\n\tlibacl.so.1 =&gt; \/lib64\/libacl.so.1 (0x0000003b75e00000)\r\n\tlibc.so.6 =&gt; \/lib64\/libc.so.6 (0x0000003b70600000)\r\n\tlibdl.so.2 =&gt; \/lib64\/libdl.so.2 (0x0000003b70a00000)\r\n\t\/lib64\/ld-linux-x86-64.so.2 (0x0000561abfc09000)\r\n\tlibpthread.so.0 =&gt; \/lib64\/libpthread.so.0 (0x0000003b70e00000)\r\n\tlibattr.so.1 =&gt; \/lib64\/libattr.so.1 (0x0000003b75600000)\r\n<\/pre>\n<p>Because shared libraries can exist in many different directories, searching through all of these directories when a program is launched would be greatly inefficient: which is one of the likely disadvantages of dynamic libraries. Therefore a mechanism of caching employed, performed by a the program\u00a0<strong>ldconfig<\/strong>.<\/p>\n<p>By default,\u00a0<strong>ldconfig<\/strong>\u00a0reads the content of\u00a0<strong>\/etc\/ld.so.conf<\/strong>, creates the appropriate symbolic links in the dynamic link directories, and then writes a cache to\u00a0<strong>\/etc\/ld.so.cache<\/strong>\u00a0which is then easily used by other programs.<\/p>\n<p>This is very important especially when you have just installed new shared libraries or created your own, or created new library directories. You need to run\u00a0<strong>ldconfig<\/strong>\u00a0command to effect the changes.<\/p>\n<pre># ldconfig\r\nOR\r\n# ldconfig -v \t#shows files and directories it works with\r\n<\/pre>\n<p>After creating your shared library, you need to install it. You can either move it into any of the standard directories mentioned above, and run the\u00a0<strong>ldconfig<\/strong>\u00a0command.<\/p>\n<p>Alternatively, run the following command to create symbolic links from the\u00a0<strong>soname<\/strong>\u00a0to the filename:<\/p>\n<pre># ldconfig -n \/path\/to\/your\/shared\/libraries\r\n<\/pre>\n<p>To get started with creating your own libraries, check out this guide from\u00a0<a href=\"http:\/\/tldp.org\/HOWTO\/Program-Library-HOWTO\/shared-libraries.html\" target=\"_blank\" rel=\"nofollow noopener\">The Linux Documentation Project(TLDP)<\/a>.<\/p>\n<p>Thats all for now! In this article, we gave you an introduction to libraries, explained shared libraries and how to manage them in Linux. If you have any queries or additional ideas to share, use the comment form below.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/understanding-shared-libraries-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In programming, a library is an assortment of pre-compiled pieces of code that can be reused in a program. Libraries simplify life for programmers, in that they provide reusable functions, routines, classes, data structures and so on (written by a another programmer), which they can use in their programs. For instance, if you are building &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/01\/understanding-shared-libraries-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Understanding Shared Libraries in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13197","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13197"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13197\/revisions"}],"predecessor-version":[{"id":13198,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13197\/revisions\/13198"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}