Here is real time example of creation of static library and its usage, explained in simple steps.
Creation of static library with example
Creation of Static Libraries In Linux Simple Steps
1. Create Test Directory
Create working test directory as: /home/test/so
2. Choose Library Files
library file name: add.c
int add(int a, int b)
{
return (a+b);
}
3. Compile
Now Compile Static Library with -shared option
gcc -c add.c
gcc -shared -o libadd.so add.o
4. Usage of Static Library
Here is example application uses this library.
filename: main.c
int main(void)
{
printf("result: %d", add(1,2));
return 0;
}
5. Compile Example Code
gcc main.c -o main -L <path where the library file is there, in our example it is libadd.so> -l add
(in last option -l add we have to specify library name. name of the library is, removing the "lib"
from libadd. i.e. how gcc takes.)
example: gcc main.c -o main -L /home/test/so -ladd
6. export LD_LIBRARY_PATH=<directory where your .so file is>
if you copy the .so file to /lib directory no need to set this environment variable
Tags static library, Linux, My Notes, C Programs, Embedded Linux.
Creation of static library with example
Creation of Static Libraries In Linux Simple Steps
1. Create Test Directory
Create working test directory as: /home/test/so
2. Choose Library Files
library file name: add.c
int add(int a, int b)
{
return (a+b);
}
3. Compile
Now Compile Static Library with -shared option
gcc -c add.c
gcc -shared -o libadd.so add.o
4. Usage of Static Library
Here is example application uses this library.
filename: main.c
int main(void)
{
printf("result: %d", add(1,2));
return 0;
}
5. Compile Example Code
gcc main.c -o main -L <path where the library file is there, in our example it is libadd.so> -l add
(in last option -l add we have to specify library name. name of the library is, removing the "lib"
from libadd. i.e. how gcc takes.)
example: gcc main.c -o main -L /home/test/so -ladd
6. export LD_LIBRARY_PATH=<directory where your .so file is>
if you copy the .so file to /lib directory no need to set this environment variable
Tags static library, Linux, My Notes, C Programs, Embedded Linux.
Comments