Static and Dynamic Libraries

This eliminates the size issue caused by static libraries because the final executable file will include only addresses instead of entire function implementations.Additionally, you don’t need to re-compile dynamic libraries every time you change a function since the executable has a pointer to the function instead of object code.How do we create a dynamic library?Let’s create a dynamic library using all the *.c files in our current directory..First we need to compile our C files into object code and make them position independent..We can do that using:Next, we need to create our dynamic library and add files to it..We can do this with the command:Once we’ve added files to our dynamic library, we need to verify that the shared library we’ve created contains all of the necessary dependencies..We can check that by running the command:“not found” will be displayed if your program is missing dependenciesThe program needs to know where to look for library files..Therefore, we must add the location of the dynamic library to the environmental LD_LIBRARY_PATH..We could do this with the following command:export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATHTo use this dynamic library (liball.so) we would create a program — let’s call it my_program.c..Let’s say this program uses functions from our dynamic library..We then need to compile the program the same way we did a static library:gcc -lliball -L..my_program.c -o program.After having created this new shared library, we can use the ldconfig command to create the necessary links and cache (for use by the run-time linker, ld.so) to the /etc/ld.so.conf directory.So when deciding whether to use a static or dynamic library for your next project — remember the tradeoffs..If you need to save space — you should probably use a dynamic library, if you need things to execute faster — you should probably use a static library.. More details

Leave a Reply