Dynamic Libraries C
Library functions in C language are inbuilt functions which are grouped together and placed in a common place called librar, each library function in C performs specific operation.
We can make use of these library functions to get the pre-defined output instead of writing our own code to get those outputs.
Why do you actually want to use a C library?
They work
One of the most important reasons you should use library functions is simply because they work.
These functions have gone through multiple rigorous testing and are easy to use.
The functions are optimized for performance
Since, the functions are “standard library” functions, a dedicated group of developers constantly make them better.
In the process, they are able to create the most efficient code optimized for maximum performance.
It saves considerable development time
Since the general functions like printing to a screen, calculating the square root, and many more are already written. You shouldn’t worry about creating them once again.
It saves valuable time and your code may not always be the most efficient.
The functions are portable
With ever changing real world needs, your application is expected to work every time, everywhere.
And, these library functions help you in that they do the same thing on every computer.
This saves time, effort and makes your program portable.
How do they work?
When a C program is compiled, the compiler generates object code. After generating the object code, the compiler also invokes linker. One of the main tasks for linker is to make code of library functions (eg printf(), scanf(), sqrt(), ..etc) available to your program. A linker can accomplish this task in two ways, by copying the code of library function to your object code, or by making some arrangements so that the complete code of library functions is not copied, but made available at run-time.
Static libraries, while reusable in multiple programs, are locked into a program at compile time, and is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory
Dynamic, or shared libraries on the other hand, exist as separate files outside of the executable file, also Dynamic Linking doesn’t require the code to be copied, it is done by just placing name of the library in the binary file. The actual linking happens when the program is run, when both the binary file and the library are in memory.
Create a Dynamic library
Linux operative systems only
this is a explanation on how make a dynamic library, for the Linux system, with commands in the terminal, if you use one compatible operating system, follow this steps
write in the terminal
gcc *.c -c -fPIC
The .c source files need to be prepared for use in a dynamic library. Since multiple programs can all use one instance of a dynamic library, the library can’t store data at fixed addresses, so make sure that the files are in the same path
By using the compiler flag -fPIC we apply this step after the compile process has generated the object code, the compiler must be told to halt and return one object file (.o) for each source file. This is done by using the -c flag.
after that, we take all the .o files and make the dynamic library
gcc *.o -shared -o [name_of_your_library.so]
The -shared flag compiled the files into a dynamic library
export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
whit this last command, we add that location to the environmental variable LD_LIBRARY_PATH, because a program needs to know where to look for library files for use them
Create a Static library
Linux operative systems only
first we need to compile all the C files that this library will contain, using this command
gcc -Wall -pedantic -Werror -Wextra -c [the_files.c_to_include]
we can create a static library whit this command
ar -rc [Your_library_name] [your_files]
If we want to see the functions that a library include we can use
ar -t [library_name]
This static library is used at link time, to satisfy the otherwise undefined references. Everything that is not satisfied at link time (compile time is not important here), must be satisfied at load time.
How can I use a static library?
Linux operative systems only
when we compile the file we need to include the library
gcc [file_to_compile_]-L. -l[library] -o [created_file_name]
How can I use a Dynamic library?
Linux operative systems only
you can compile your file whit this command
(call the library without .so)
gcc -Wall -pedantic -Werror -Wextra -L. [name_of_your_file.c] -[name_of_your_library] -o [final_name_of_your_compile_file]
This will create a program using object file, and any symbols it requires from the library. The linker attaches these parts back to the name of the library to create a name of a file to look for. Note also the usage of the '-L' flag - this flag tells the linker that libraries might be found in the given directory ('.', refering to the current directory), in addition to the standard locations where the compiler looks for system libraries.
There are differences differences between static and dynamic libraries?
A Static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.
Dynamic libraries are stored and versioned separately. It’s possible for a version of the dynamic library to be loaded that wasn’t the original one that shipped with your code if the update is considered binary compatible with the original version.
PROS & CONS STATIC LIBRARIES
Pros: no performance impact as code is always loaded in the process and is no versioning problems.
Cons: executable/process “bloat” — all the code is in your executable and is loaded upon process start; no reuse/sharing — each product has its own copy of the code.
Cons: Static libraries increase the size of the code.
PROS & CONS DYNAMIC LIBRARIES
Pros: the size of the file is usually smaller; we can run several programs which share the same library functions in memory more efficiently.
Cons: the executable files are not self-sufficient. Also, if we change the shared libraries(Dynamic Linking Libraries), the corresponding executable file may not function well.
Pros: we don’t need to compile the source files again if we modify any library functions.
Cons: lower execution time compared to static libraries