Static or Dynamic Libraries, which one to use…

Shared (dynamic) Libraries vs. Static Libraries —Differences in  performance. | LaptrinhX

Diference between Static and Dynamic Libraries

Advantages and Disadvantages of Dynamic Libraries

  1. It only needs one copy at runtime. It is dependent on the application and the library being closely available to each other.
  2. Multiple running applications use the same library without the need of each file having its own copy.
  3. However, what if the dynamic library becomes corrupt? The executable file may not work because it lives outside of the executable and is vulnerable to breaking.
  4. They hold smaller files.
  5. Dynamic libraries are linked at run-time. It does not require recompilation and relinking when the programmer makes a change.

Advantages and Disadvantages of Static Libraries

  1. Static libraries resist vulnerability because it lives inside the executable file.
  2. The speed at run-time occurs faster because its object code (binary) is in the executable file. Thus, calls made to the functions get executed quicker. Remember, the dynamic library lives outside of the executable, so calls would be made from the outside of the executable.
  3. Changes made to the files and program require relinking and recompilation.
  4. File size is much larger.

How to create libraries.

gcc -g -fPIC -Wall -Werror -Wextra -pedantic *.c -shared -o liball.so
export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
gcc -g -wall -o app app.c liball.so
ar -rc name_of_libary.a our_function.o
ranlib liball.a
gcc my_program.c -L. -lname_of_library -o my_program
  • First of all -L says “look in directory for library files” if we add a dot it means the current directory.
  • -l says “link with this library file followed the name of our library, ommiting the .a prefix of it.
  • And at last -o option followed by teh program we want to compile.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store