SlideShare a Scribd company logo
LIBRARIESLIBRARIES
Ashwanth SAshwanth S
LIBRARIES
It is a file containing compiled code that is to be
incorporated later into a program.
It is of two types:
1. Static library
2. Shared library
Static Library
● An archive (or static library) is simply a collection of
object files stored as a single file.
● Static libraries end with the ``.a'' suffix. This collection
is created using the ar (archiver) program.
● When the linker encounters an archive on the
command line, it searches the archive for all
definitions of symbols (functions or variables) that are
referenced from the object files that it has already
processed but not yet defined.
● The object files that define those symbols are extracted
from the archive and included in the final executable.
Creating a static library
● Compile: gcc -Wall -g -c prog1.c prog2.c
-Wall: include warnings
-g : include debugging info
● Create library:ar rcs libname.a prog1.o prog2.o
● List files in library: ar -t libctest.a
Libraries
● Linking with the library:
➢ gcc -o executable-name prog.c libname.a
➢ gcc -o executable-name prog.c -L/path/to/library-
directory -lname
Libraries
Shared Library
● A shared library (also known as a shared object, or as
a dynamically linked library) is similar to a archive in
that it is a grouping of object files.
● The most fundamental difference is that when a shared
library is linked into a program, the final executable
does not actually contain the code that is present in the
shared library. Instead, the executable merely contains
a reference to the shared library.
Creating a shared library
● Create library:
gcc -Wall -fPIC -c prog1.c prog2.c
gcc -shared -o libname.so prog1.o prog2.o
● You can create a seperate directory for your
shared libraries. Eg /opt/lib where opt is a
directory that is reserved for all the software
and add-on packages that are not part of the
default installation.
● You can move the created shared library to that directory.
Eg: mv libname.so /opt/lib
● If you are creating the library with version number eg
libname.so.x where x is the version number you have to
link the file as shown below.
ln -sf /opt/lib/libname.so.x /opt/lib/libname.so
● The link to /opt/lib/libname.so allows the naming
convention for the compile flag -lctest to work.
Libraries
● Compile main program and link with shared object
library:
gcc -Wall -o prog prog.c -L/opt/lib -lname
● The shared library dependencies of the executable
can be listed with the command: ldd name-of-
executable
eg: ldd prog
● The output of ldd may show something like
below:
libname.so => not found
● You can fix this by following one of the
method below:
a) export LD_LIBRARY_PATH=/opt/lib
Libraries
b) During compiling you could add rpath as below
gcc -o prog prog.c -L/opt/lib -lname 
-Wl,-rpath=/opt/lib
c) You can add the path of the library in
etc/ld.so.conf.d/libc.conf
● To update the cache use
sudo ldconfig
Libraries
● Now if you check the dependencies the not found
would have disappeared.
● In LD_LIBRARY_METHOD the environmental
variable vanishes once you close the terminal so
you have to specify the path again and again
● In rpath the path is added to the executable so you
dont have to mention it again for that one.
● If you have added the library path in libc.conf the
libraries will be searched there automatically.
● Note:
➢ Suppose that both libname.a and libname.so are
available.Then the linker must choose one of the
libraries and not the other.
➢ The linker searches each directory (first those
specified with -L options, and then those in the
standard directories).
➢ When the linker finds a directory that contains either
libtest.a or libtest.so, the linker stops searching the
directories.
➢ If only one of the two variants is present in the directory,
the linkerchooses that variant.
➢ Otherwise, the linker chooses the shared library version,
unless you explicitly instruct it otherwise.You can use the
-static option to demand static archives.
➢ For example, the following line will use the libname.a
archive, even if the libname.so shared library is also
available:
gcc -static -o prog prog.c -L/opt/lib –lname
● These shared libraries are dynamically linked.
● Even though the dynamic linker does a lot of the work for
shared libraries, the traditional linker still has a role to play
in creating the executable.
● The traditional linker needs to leave a pointer in the
executable so that the dynamic linker knows what library
will satisfy the dependencies at runtime.
● The dynamic section of the executable requires a
NEEDED entry for each shared library that the executable
depends on.
Libraries
● The dynamic linker is the program that manages
shared dynamic libraries on behalf of an
executable.
● The essential part of the dynamic linker is fixing up
addresses at runtime, which is the only time you
can know for certain where you are loaded in
memory. A relocation can simply be thought of as
a note that a particular address will need to be fixed
at load time.
● Shared libraries can also be loaded at times other than
during the startup of a program. They're particularly
useful for implementing plugins. Eg adobe plugin in
browser..
● To use dynamic loading functions,include the
<dlfcn.h> header file and link with the –ldl option to
pick up the libdl library.
Functions in dynamic loaading
● The dlopen function opens a library
void * dlopen(const char *filename, int flag);
eg:
dlopen("/opt/lib/libname.so", RTLD_LAZY);
RTLD_LAZY: If specfied there is no concern
about unresolved symbols until they are
referenced.
RTLD_NOW: All unresolved symbols are resolved
when dlopen() is called.
● dlerror()
Errors can be reported by calling dlerror(), which
returns a string describing the error from the last
call to dlopen(), dlsym(), or dlclose(). One point to
note is that after calling dlerror(), future calls to
dlerror() will return NULL until another error has
been encountered.
● dlsym()
The return value from the dlopen function is a void
* that is used as a handle for the shared library.You
can pass this value to the dlsym function to obtain
the address of a function that has been loaded with
the shared library.
● dlclose()
The dlclose function unloads the shared library.
● The executable is created for dynamic loading
as below
gcc -rdynamic -o progdl progdl.c -ldl
● Thus library is dynamically loaded.
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include "ctest.h"
int main(int argc, char **argv)
{
void *lib_handle;
double (*fn)(int *);
int x;
char *error;
lib_handle = dlopen("/opt/lib/libctest.so",
RTLD_LAZY);
if (lib_handle==NULL)
{
fprintf(stderr, "%sn", dlerror());
exit(1);
}
fn = dlsym(lib_handle, "ctest1");
if ((error = dlerror()) != NULL)
{
fprintf(stderr, "%sn", error);
exit(1);
}
(*fn)(&x);
printf("Valx=%dn",x);
dlclose(lib_handle);
return 0;
}
Benefits of Static Libraries
● Static libraries cannot eliminate duplicated
code in the system. However, there are
other benefits to using static libraries. Some
of these benefits are as follows:
● Static libraries are simple to use.
● The static library code does not need to be
position-independent code.
Benefits of Shared Libraries
● Code sharing saves system resources.
● Several programs that depend on a common
shared library can be fixed all at once by
replacing the common shared library.
● The environment can be modified to use a
shared library.
● Programs can be written to load dynamic
libraries without any prior arrangement at link
time.

More Related Content

PDF
Whirlwind tour of the Runtime Dynamic Linker
ODP
LD_PRELOAD Exploitation - DC9723
PDF
Dynamic Linker
PPT
A hands-on introduction to the ELF Object file format
PDF
Compilation and Execution
PDF
FISL XIV - The ELF File Format and the Linux Loader
PPTX
Linker and loader upload
PPT
Compilation
Whirlwind tour of the Runtime Dynamic Linker
LD_PRELOAD Exploitation - DC9723
Dynamic Linker
A hands-on introduction to the ELF Object file format
Compilation and Execution
FISL XIV - The ELF File Format and the Linux Loader
Linker and loader upload
Compilation

What's hot (20)

PDF
Self-Hosted Scripting in Guile
PDF
Introduction to the LLVM Compiler System
PDF
Program Structure in GNU/Linux (ELF Format)
PPT
PDF
Assembler
PPT
嵌入式Linux課程-GNU Toolchain
PDF
LLVM Compiler - Link Time Optimization
PDF
.NET Core Blimey! (Shropshire Devs Mar 2016)
TXT
Mbuild help
PDF
Beginning with Composer - Dependency manager in php
PPTX
Robot operating system [ROS]
PDF
Understanding Implicits in Scala
PDF
Functional programming in Scala
PPT
Indic threads pune12-apache-crunch
PDF
CNIT 126 Ch 7: Analyzing Malicious Windows Programs
PPTX
Java.util.concurrent.concurrent hashmap
PPTX
Python Streaming Pipelines with Beam on Flink
PPT
web programming Unit VI PPT by Bhavsingh Maloth
PDF
Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019
PPTX
Apache Crunch
Self-Hosted Scripting in Guile
Introduction to the LLVM Compiler System
Program Structure in GNU/Linux (ELF Format)
Assembler
嵌入式Linux課程-GNU Toolchain
LLVM Compiler - Link Time Optimization
.NET Core Blimey! (Shropshire Devs Mar 2016)
Mbuild help
Beginning with Composer - Dependency manager in php
Robot operating system [ROS]
Understanding Implicits in Scala
Functional programming in Scala
Indic threads pune12-apache-crunch
CNIT 126 Ch 7: Analyzing Malicious Windows Programs
Java.util.concurrent.concurrent hashmap
Python Streaming Pipelines with Beam on Flink
web programming Unit VI PPT by Bhavsingh Maloth
Python Streaming Pipelines on Flink - Beam Meetup at Lyft 2019
Apache Crunch
Ad

Viewers also liked (7)

PDF
Dough Slapper or Dough Master: Building Reports for Papa John's
PDF
Práctica 1b
PDF
Manual da Churrasqueira a Gás de embutir - P
PDF
Portfolio Management Categorization Optimization and Re-calibration
PDF
Idade Moderna
PPTX
Medical-PPT
DOCX
Acta entrega recepcion de cello
Dough Slapper or Dough Master: Building Reports for Papa John's
Práctica 1b
Manual da Churrasqueira a Gás de embutir - P
Portfolio Management Categorization Optimization and Re-calibration
Idade Moderna
Medical-PPT
Acta entrega recepcion de cello
Ad

Similar to Libraries (20)

PPT
From gcc to the autotools
PPT
Advanced c programming in Linux
PPTX
C++ shared libraries and loading
PPT
PPT
101 2.3 manage shared libraries
PPT
Introduction to libraries in india and china
PPT
101 2.3 manage shared libraries
PPT
2.3 manage shared libraries
PDF
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
PDF
Android Variants, Hacks, Tricks and Resources
PDF
Development and deployment with composer and kite
PPTX
Composer namespacing
PDF
Livecode widget course
ODP
Working with Shared Libraries in Perl
PPTX
PHP Dependency Management with Composer
PPTX
SECR'13 Lightweight linux shared libraries profiling
PPTX
Autotools pratical training
PDF
Composer Helpdesk
PPTX
Linkers
From gcc to the autotools
Advanced c programming in Linux
C++ shared libraries and loading
101 2.3 manage shared libraries
Introduction to libraries in india and china
101 2.3 manage shared libraries
2.3 manage shared libraries
Embedded Systems: Lecture 13: Introduction to GNU Toolchain (Build Tools)
Android Variants, Hacks, Tricks and Resources
Development and deployment with composer and kite
Composer namespacing
Livecode widget course
Working with Shared Libraries in Perl
PHP Dependency Management with Composer
SECR'13 Lightweight linux shared libraries profiling
Autotools pratical training
Composer Helpdesk
Linkers

Recently uploaded (20)

PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Transform Your Business with a Software ERP System
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PPTX
history of c programming in notes for students .pptx
PPTX
Patient Appointment Booking in Odoo with online payment
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Download FL Studio Crack Latest version 2025 ?
PPTX
assetexplorer- product-overview - presentation
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PDF
17 Powerful Integrations Your Next-Gen MLM Software Needs
PDF
Designing Intelligence for the Shop Floor.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Design an Analysis of Algorithms I-SECS-1021-03
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Transform Your Business with a Software ERP System
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Digital Systems & Binary Numbers (comprehensive )
Monitoring Stack: Grafana, Loki & Promtail
history of c programming in notes for students .pptx
Patient Appointment Booking in Odoo with online payment
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
iTop VPN 6.5.0 Crack + License Key 2025 (Premium Version)
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
CHAPTER 2 - PM Management and IT Context
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Download FL Studio Crack Latest version 2025 ?
assetexplorer- product-overview - presentation
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
17 Powerful Integrations Your Next-Gen MLM Software Needs
Designing Intelligence for the Shop Floor.pdf

Libraries

  • 2. LIBRARIES It is a file containing compiled code that is to be incorporated later into a program. It is of two types: 1. Static library 2. Shared library
  • 3. Static Library ● An archive (or static library) is simply a collection of object files stored as a single file. ● Static libraries end with the ``.a'' suffix. This collection is created using the ar (archiver) program. ● When the linker encounters an archive on the command line, it searches the archive for all definitions of symbols (functions or variables) that are referenced from the object files that it has already processed but not yet defined. ● The object files that define those symbols are extracted from the archive and included in the final executable.
  • 4. Creating a static library ● Compile: gcc -Wall -g -c prog1.c prog2.c -Wall: include warnings -g : include debugging info ● Create library:ar rcs libname.a prog1.o prog2.o ● List files in library: ar -t libctest.a
  • 6. ● Linking with the library: ➢ gcc -o executable-name prog.c libname.a ➢ gcc -o executable-name prog.c -L/path/to/library- directory -lname
  • 8. Shared Library ● A shared library (also known as a shared object, or as a dynamically linked library) is similar to a archive in that it is a grouping of object files. ● The most fundamental difference is that when a shared library is linked into a program, the final executable does not actually contain the code that is present in the shared library. Instead, the executable merely contains a reference to the shared library.
  • 9. Creating a shared library ● Create library: gcc -Wall -fPIC -c prog1.c prog2.c gcc -shared -o libname.so prog1.o prog2.o ● You can create a seperate directory for your shared libraries. Eg /opt/lib where opt is a directory that is reserved for all the software and add-on packages that are not part of the default installation.
  • 10. ● You can move the created shared library to that directory. Eg: mv libname.so /opt/lib ● If you are creating the library with version number eg libname.so.x where x is the version number you have to link the file as shown below. ln -sf /opt/lib/libname.so.x /opt/lib/libname.so ● The link to /opt/lib/libname.so allows the naming convention for the compile flag -lctest to work.
  • 12. ● Compile main program and link with shared object library: gcc -Wall -o prog prog.c -L/opt/lib -lname ● The shared library dependencies of the executable can be listed with the command: ldd name-of- executable eg: ldd prog
  • 13. ● The output of ldd may show something like below: libname.so => not found ● You can fix this by following one of the method below: a) export LD_LIBRARY_PATH=/opt/lib
  • 15. b) During compiling you could add rpath as below gcc -o prog prog.c -L/opt/lib -lname -Wl,-rpath=/opt/lib c) You can add the path of the library in etc/ld.so.conf.d/libc.conf ● To update the cache use sudo ldconfig
  • 17. ● Now if you check the dependencies the not found would have disappeared. ● In LD_LIBRARY_METHOD the environmental variable vanishes once you close the terminal so you have to specify the path again and again ● In rpath the path is added to the executable so you dont have to mention it again for that one.
  • 18. ● If you have added the library path in libc.conf the libraries will be searched there automatically. ● Note: ➢ Suppose that both libname.a and libname.so are available.Then the linker must choose one of the libraries and not the other. ➢ The linker searches each directory (first those specified with -L options, and then those in the standard directories).
  • 19. ➢ When the linker finds a directory that contains either libtest.a or libtest.so, the linker stops searching the directories. ➢ If only one of the two variants is present in the directory, the linkerchooses that variant. ➢ Otherwise, the linker chooses the shared library version, unless you explicitly instruct it otherwise.You can use the -static option to demand static archives. ➢ For example, the following line will use the libname.a archive, even if the libname.so shared library is also available: gcc -static -o prog prog.c -L/opt/lib –lname
  • 20. ● These shared libraries are dynamically linked. ● Even though the dynamic linker does a lot of the work for shared libraries, the traditional linker still has a role to play in creating the executable. ● The traditional linker needs to leave a pointer in the executable so that the dynamic linker knows what library will satisfy the dependencies at runtime. ● The dynamic section of the executable requires a NEEDED entry for each shared library that the executable depends on.
  • 22. ● The dynamic linker is the program that manages shared dynamic libraries on behalf of an executable. ● The essential part of the dynamic linker is fixing up addresses at runtime, which is the only time you can know for certain where you are loaded in memory. A relocation can simply be thought of as a note that a particular address will need to be fixed at load time.
  • 23. ● Shared libraries can also be loaded at times other than during the startup of a program. They're particularly useful for implementing plugins. Eg adobe plugin in browser.. ● To use dynamic loading functions,include the <dlfcn.h> header file and link with the –ldl option to pick up the libdl library.
  • 24. Functions in dynamic loaading ● The dlopen function opens a library void * dlopen(const char *filename, int flag); eg: dlopen("/opt/lib/libname.so", RTLD_LAZY); RTLD_LAZY: If specfied there is no concern about unresolved symbols until they are referenced.
  • 25. RTLD_NOW: All unresolved symbols are resolved when dlopen() is called. ● dlerror() Errors can be reported by calling dlerror(), which returns a string describing the error from the last call to dlopen(), dlsym(), or dlclose(). One point to note is that after calling dlerror(), future calls to dlerror() will return NULL until another error has been encountered.
  • 26. ● dlsym() The return value from the dlopen function is a void * that is used as a handle for the shared library.You can pass this value to the dlsym function to obtain the address of a function that has been loaded with the shared library. ● dlclose() The dlclose function unloads the shared library.
  • 27. ● The executable is created for dynamic loading as below gcc -rdynamic -o progdl progdl.c -ldl ● Thus library is dynamically loaded.
  • 28. #include <stdio.h> #include <stdlib.h> #include <dlfcn.h> #include "ctest.h" int main(int argc, char **argv) { void *lib_handle; double (*fn)(int *); int x; char *error;
  • 29. lib_handle = dlopen("/opt/lib/libctest.so", RTLD_LAZY); if (lib_handle==NULL) { fprintf(stderr, "%sn", dlerror()); exit(1); } fn = dlsym(lib_handle, "ctest1");
  • 30. if ((error = dlerror()) != NULL) { fprintf(stderr, "%sn", error); exit(1); } (*fn)(&x); printf("Valx=%dn",x); dlclose(lib_handle); return 0; }
  • 31. Benefits of Static Libraries ● Static libraries cannot eliminate duplicated code in the system. However, there are other benefits to using static libraries. Some of these benefits are as follows: ● Static libraries are simple to use. ● The static library code does not need to be position-independent code.
  • 32. Benefits of Shared Libraries ● Code sharing saves system resources. ● Several programs that depend on a common shared library can be fixed all at once by replacing the common shared library. ● The environment can be modified to use a shared library. ● Programs can be written to load dynamic libraries without any prior arrangement at link time.