C static libraries.
This poster will answer general questions about static libraries in C. Sit down and read carefully
C Standard library functions or simply C Library functions are inbuilt functions in C programming. The prototype and data definitions of these functions are present in their respective header files. To use these functions we need to include the header file in our program. For example,
If you want to use the printf()
function, the header file <stdio.h>
should be included.
#include <stdio.h>
int main()
{
printf("Catch me if you can.");
}
If you try to use printf()
without including the stdio.h
header file, you will get an error.
Advantages of Using C library functions
- They work
- The functions are optimized for performance
- It saves considerable development time
- The functions are portable
How to create them?
Follow this steps for create a static library.
1- Compile library files.
gcc -c lib_mylib.c -o lib_mylib.o
2- Create static library. This step is to bundle multiple object files in one static library (see ar for details). The output of this step is static library.
ar rcs lib_mylib.a lib_mylib.o
3- Now our static library is ready to use.
How to use them?
when you have created your .o file from where to run your library link the compiled driver program to the static library. Note that -L. is used to tell that the static library is in current folder .
gcc -o <file_name> <file_name>.o -L. -l_mylib
and finally run the program
./<file_name>