Dynamic (shared) vs. Shared Library Performance

Shared Library PerformanceConnor BreretonBlockedUnblockFollowFollowingDec 10Trinity College — Dublin, IrelandTable of Contents:What is a shared library?How to create a shared library?Why are they useful?How is it different than a static library?So what are they?A shared libraries is a library that dynamic links during compilation when a user compiles a .c file..Static and dynamic linking are two processes of collecting and combining multiple object files in order to create a single executable file..The main difference between the two is the type of linking they do when creating an executable file.Contrary to a static library, a dynamic library performs the linking process as a program is executed in Linux..Moreover, dynamic libraries are loaded into memory by programs when they are executed..During compilation a shared libraries machine code is cached locally and has a version control of sorts.Let’s say you create a shared library and then run some tests..Next, you decide to add some more code to your functions in the shared library..Well, when you recompile, the compilation process just appends the recent changes to the cache versus having to recompile everything all over again as you would in a static library creation.SrcHow to create a shared library?So lets say we have a couple of files that we want to turn into a shared library…file0.c file1.c file2.c and a header file fheader.heach of those functions serve their own purpose and their prototypes are included in the header file.To create the shared library lets run this command$ gcc -fPIC -Wall -Werror -Wextra -pedantic *.c -shared -o libfile.soThe -fPIC flag enables position independent code, which allows the code to be located at any virtual address at runtime.-Wall enables all warnings our compiler could throw-Werror turns all warnings into errors (won’t compile)-Wextra ensures that type comparisons are valid-pedantic makes sure that the code follows ISO standardsThe -shared flag indicates that we want to create a shared library which has the file extension .so..The -o flag allows us to name the output file outselves.Final result: libfile.soWhy are they useful?So going back to our files example in the previous section lets say we created another file called file3.c that relies on another file’s prototype..We want to add this new code to the repository we have but we need to compile it first..We would want to use a dynamic library since we are probably going to be adding more files in the future..See below:$ gcc -Wall -Werror -Wextra -pedantic -L..file3.c libfile -o foo-L.. More details

Leave a Reply