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.

For both of them you should include a header file that contains all your functions prototype.

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
  • -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.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A STEP-by-STEP Guide to Getting a Google Internship

Top Programming Languages for Backend Development in 2020

Remote Configuration — A/B Testing | Use And Analyze New Features Without Any Update

Introducing The Glasswall Camera Workbench

Why I love being a Product Manager

Designing The Layout of a Family Tree #2

Resolve  “CIDR Address overlaps with existing Subnet CIDR” with little Networking knowledge

Receipt/Invoice Close Tolerance explanation in Purchase Order/Oracle Fusion Procurement

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
Chloé Dumit

Chloé Dumit

More from Medium

Step #4: Achieving Your New Year’s Resolution

Robert Eggers’ ‘The Witch’ Through Goya’s Darkened Canvas

CS373 Spring 2022: Kyle Kamka

Pamela Nkonge| Investigator, Office of Internal Oversight Services, United Nations Office in…