The C Static Libraries
Library Function in C:
- Library functions in C language are inbuilt functions which are grouped together and placed in a common place called library.
- 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.
- All C standard library functions are declared in many header files which are saved as file_name.h.
- We are including these header files in our C program using “#include<file_name.h>” command to make use of the functions those are declared in the header files.
- When we include header files in our C program using “#include<filename.h>” command, all C code of the header files are included in C program. Then, this C program is compiled by compiler and executed.
Why to use C library functions
There are many library functions available in C programming to help you write a good and efficient program. But, why should you use it?
1. 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.
2. 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.
3. 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.
4. 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 libraries work
Once the libraries are created, they can be linked with the file containing the main function, or entry point, with gcc. Then, the code of the functions used in the program will be linked into the executable program, and it will be ready to run!
How to create Static libraries
Let’s use as an example the file “my_file.c”.We need to create object files first. This is done with the following commands:
$ gcc -c my_file.c
The ‘-c’ option makes sure the compilation process stops before the linker, and creates the ‘my_file.o’ file
Now we can create the ‘mylib’ library with our object file. Here’s how:
$ ar rc libmylib.a my_file.o
We use the command ar to create a ‘.a’ file (a stands for archive)
How to use libraries
Now that we have our library, we have to use is to compile and create our program. Let’s say our entry point is in the ‘main.c’ file, and we want to name our program ‘test’. Here’s the command to compile the program with our library :
$ gcc -L. main.c -lmylib -o test
The ‘-L’ flag tells the compiler where it needs to look for the library, so in this case where the library is in the current working directory, we just use a dot. the ‘-lmylib’ tells the compiler to link the code in main.c with the code in the library my_lib. Finally, the ‘-o’ flag allows us to give a name to the executable, in this case it will be ‘test’.