\section{Libraries}

\begin{frame}
   {Static and Shared Libraries}

   \begin{itemize}
      \item
      \textbf{Static Libraries}
      \begin{itemize}
         \item Code compiled into programs
         \item Do not get updated library features without
         recompile
      \end{itemize}

      \item
      \textbf{Shared Libraries}
      \begin{itemize}
         \item Code loaded at run time
         \item Updated library code is used automatically
      \end{itemize}

      \item Shared libraries are more efficient
      \item Also called \textbf{DLL}s (\textbf{D}ynamic
      \textbf{L}ink \textbf{L}ibrary).
   \end{itemize}
   \begin{cmd}
c8:/usr/lib64>ls -l libpthread.*
   \end{cmd}
   \begin{responsevar}{\normalsize}
-rwxr-xr-x 1 root root 320504 Jan  7 11:20 libpthread-2.28.so
-rw-r--r-- 1 root root 993146 Jan  7 11:21 libpthread.a
lrwxrwxrwx 1 root root     27 Jan  7 11:08 libpthread.so -> ../../lib64/libpthread.so.0
lrwxrwxrwx 1 root root     18 Jan  7 11:09 libpthread.so.0 -> libpthread-2.28.so
   \end{responsevar}
\end{frame}

\cprotect\note{

   Programs are built using \textbf{libraries} of code,
   developed for multiple purposes and used and reused in
   many contexts.

   There are two types of libraries:

   \begin{itemize}
      \item
      \textbf{Static}

      The code for the library functions is inserted in the
      program at \textbf{compile time}, and does not change
      thereafter, even if the library is updated.
      \item
      \textbf{Shared}

      The code for the library functions is loaded into the
      program at \textbf{run time}, and if the library is
      changed later, the running program runs with the new
      library modifications.
   \end{itemize}

   Using shared libraries is more efficient because they
   can be used by many applications at once; memory usage,
   executable sizes, and application load time are reduced.

   Shared Libraries are also called \textbf{DLL}s
   (\textbf{D}ynamic \textbf{L}ink \textbf{L}ibrary).

}


\begin{frame}
   {Shared Library Versions}

   \begin{itemize}
      \item Careful versioning needed
      \item Avoid \textbf{DLL Hell}
      \item Programs can request specific \textbf{major} and/or
      \textbf{minor} library versions
      \item Some applications use only static libraries to avoid
      problems, but can lag in bug and security fixes
   \end{itemize}

\end{frame}

\cprotect\note{

   Shared libraries need to be carefully versioned.  If
   there is a significant change to the library and a
   program is not equipped to handle it, serious problems
   can be expected.  This is sometimes knows as \textbf{DLL
      Hell}.

   Therefore, programs can request a specific \textbf{major}
   library version rather than the latest one on the system.
   However, usually the program will use the latest
   \textbf{minor} version available.

   Some application providers will use static libraries
   bundled into the program to avoid these problems.
   However, if there are improvements or bugs and security
   holes fixed in the libraries, they may not make it into
   the applications in timely fashion.

   Shared libraries have the extension
   \textbf{.so}. Typically the full name is something like
   \texttt{libc.so.N} where \texttt{N} is a major version
   number.

   Under \textbf{Linux} shared libraries are carefully
   versioned. For example:

   \begin{cmd}
c8:/usr/lib64>ls -l libcrypt.*
   \end{cmd}
   \begin{response}
-rw-r--r-- 1 root root 233788 May 10  2019 libcrypt.a
lrwxrwxrwx 1 root root     17 Aug 12  2018 libcrypt.so -> libcrypt.so.1.1.0
lrwxrwxrwx 1 root root     17 Aug 12  2018 libcrypt.so.1 -> libcrypt.so.1.1.0
-rwxr-xr-x 1 root root 142712 Aug 12  2018 libcrypt.so.1.1.0
   \end{response}
   so a program that just asks for \texttt{libcrypt} gets
   \filelink{libcrypt.so} and the others for specific major
   and minor versions.  Also note \file{libycrypt.a} is
   available for a program that wants to link to the static
   version of the library.

}


\begin{frame}
   {Finding Shared Libraries}

   \begin{itemize}

      \item
      Shared libraries need to found at run time
      \item
      \textbf{ldd} shows shared libraries used by program
      \item
      \textbf{ldconfig} uses \filelink{/etc/ld.so.conf} to
      cache where to look.
      \begin{itemize}
         \item Run at boot or any time later
      \end{itemize}
      \item
      Can specify \verb?LD_LIBRARY_PATH? to override default
      directory search
   \end{itemize}
\end{frame}

\cprotect\note {

   \textbf{ldd} ascertains which shared libraries an
   executable requires, showing the \textbf{soname} of the
   library and what file it points to:

   \begin{cmd}
student@ubuntu: $ ldd /usr/bin/vi
   \end{cmd}
   \begin{responsevar}{\scriptsize}
        linux-vdso.so.1 (0x00007ffd2ae93000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f9c9275a000)
        libtinfo.so.6 => /lib/x86_64-linux-gnu/libtinfo.so.6 (0x00007f9c9272a000)
        libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f9c926ff000)
        libcanberra.so.0 => /lib/x86_64-linux-gnu/libcanberra.so.0 (0x00007f9c926ec000)
        libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f9c926e1000)
        libgpm.so.2 => /lib/x86_64-linux-gnu/libgpm.so.2 (0x00007f9c924db000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f9c924d3000)
        libpython3.8.so.1.0 => /lib/x86_64-linux-gnu/libpython3.8.so.1.0 (0x00007f9c91f7d000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f9c91f5a000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9c91d68000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f9c92b98000)
        libpcre2-8.so.0 => /lib/x86_64-linux-gnu/libpcre2-8.so.0 (0x00007f9c91cd8000)
        libvorbisfile.so.3 => /lib/x86_64-linux-gnu/libvorbisfile.so.3 (0x00007f9c91ccd000)
        libtdb.so.1 => /lib/x86_64-linux-gnu/libtdb.so.1 (0x00007f9c91cb1000)
        libltdl.so.7 => /lib/x86_64-linux-gnu/libltdl.so.7 (0x00007f9c91ca6000)
        libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007f9c91c78000)
        libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f9c91c5c000)
        libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007f9c91c57000)
        libvorbis.so.0 => /lib/x86_64-linux-gnu/libvorbis.so.0 (0x00007f9c91c27000)
        libogg.so.0 => /lib/x86_64-linux-gnu/libogg.so.0 (0x00007f9c91c1a000)
   \end{responsevar}


   %   \begin{figure}[H]
   %      \includegraphics[width=3.9in]{IMAGES/lddvi}

   %%      \caption{Using ldd}
   %   \end{figure}

   \textbf{ldconfig} is generally run at boot time (but can be
   run anytime), and uses the \filelink{/etc/ld.so.conf},
   which lists the directories that will be searched for
   shared libraries.

   Besides searching the data base built up by
   \textbf{ldconfig}, the linker will first search any
   directories specified in the environment variable
   \verb?LD_LIBRARY_PATH?, a colon separated list of
   directories, as in the \texttt{PATH} variable.  So you
   can do either of:
   \begin{cmd}
$ LD_LIBRARY_PATH=$HOME/foo/lib ; foo [args]
$ LD_LIBRARY_PATH=$HOME/foo/lib   foo [args]
   \end{cmd}
}

