Differences between static and dynamic libraries

Daniel felipe Escobar chavez
3 min readMay 4, 2021

In c when we compile a file we create an executable file which we can use to test the functions we create.
all good for now but there is a way to store these functions in an organized way and use them as we want and that is achieved with libraries.

Now we are going to the important thing, there are two types of libraries to work with and these are dynamic and static and now we will see the differences

STATIC:

  • If you want to make changes to the files that are saved, you have to re-link and compile to update the changes.
  • You can save bigger files but it takes longer to run.
  • Never has compatibility problems.

DYNAMIC:

  • no need to recompile the executable.
  • Smaller files are secured but it takes less time to execute.
  • depends on a compatible library and if this is removed it will not work

taking into account all this now we will see how to create these libraries and use them to create our functions

HOW CREATE STATIC:

the first step is to have our function in a .c file, we can have any function in our file as long as it works

example.c

here my function is saved in my example.c file.

The next thing will be to pass that file to a .o file using the following command so that the library that we create can store it

gcc -Wall -pedantic -Werror -Wextra -c *.c

we will do it like this in case there are several functions

In the next one, it will be to create the library and put in it all the files that end in .o, the library will be called libh.a

ar -rc libh.a *.o

then to check that we did it right we use this command that will show our .o files

ar -t libh.a

now to make our library run we make use of

ranlib libh

to use the library we will create a main.c file to which we will put one of our functions

gcc main.c -L. -lh -o alpha

the -L is the address and the lib is changed to l to shorten the command.

HOW CREATE DYNAMIC:

gcc *.c -c -fPIC

Once having our functions done, the first step would be to compile all the files that we have as .o files for the library and the -fPIC flag is used so that the code is independent and does not matter where the code is loaded

gcc *.o -shared -o liball.so

Then we do another compilation where we take all the .o that would refer to our files, the -shares flag has the objective of producing a shared object which can then be linked with other objects to form an executable. we finally use the name liball.so for the library.

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

finally we use this environment variable to give it the path of the files in the library

CONCLUSION:

In conclusion, both libraries have their advantages and disadvantages, but despite that it is good to start using them to handle the functions in a simple way.
with all this the blog ends here, see you later!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Responses (1)

Write a response

now but

now, but

--