Adding Static Library
In this section, you are going to learn
What is static library ?
How to create static library ?
How to link static library with object files ?
Let us answer few basic questions about static library
What is the use of library ?
See Answer
It is a collection of pre-compiled functions that can be used by a program.
Used to reduce the redundancy of code.
What are the types of library ?
See Answer
- Two types of library:
Static Library
Shared Library
What is Static Library ?
See Answer
Collection of compiled object code that can be linked directly to the program at the compile time.
How to link the object files to generate the static library ?
See Answer
ar command is used to generate the static library.
ar rcs <YOUR_LIBRARY_NAME> <OBJECT_FILE_NAMES>
What command is used to link the generated static library in compile time ?
See Answer
To use the static library we must use this gcc command
gcc sample.c -o sample -L<YOUR_LIBRARY_FILE_PATH> -l<YOUR_LIBRARY_NAME>
-L specifies the directory path where the library is located.
-l links with the specified library
In ar command what option “t” does ?
See Answer
option t is used to display the table listing of the object files linked with the static library.
$ ar t libmathfunc.a
add.o
mul.o
sub.o
What “nm” command does ?
See Answer
nm command is used to display information about symbol in objects file or binaries.
$ nm libmathfunc.a
add.o:
0000000000000000 T sum
mul.o:
0000000000000000 T mul
sub.o:
0000000000000000 T sub
Why the size of executable file linked with static library file is greater when compared to shared library file ?
See Answer
The linking of static library file happens at compile time.
This results in copying the whole static library file with all its function to the executable bin.
Let us now explore it in depth !
Step 1: Create an header file with all the necessary function prototypes.
1int sum(int a,int b);
2int sub(int a,int b);
3int mul(int a,int b);
Step 2: Create source code file with function definitions for all the functions defined in header file.
1int sum (int a,int b)
2{
3 return a+b;
4}
1int sub (int a, int b)
2{
3 return a-b;
4}
1int mul (int a, int b)
2{
3 return a*b;
4}
Step 3: Generate Static Library using ar command.
$ gcc -c add.c $ gcc -c sub.c $ gcc -c mul.c $ ar rcs libmathfunc.a add.o sub.o mul.o
Step 4: Create an application and use the functions being defined in the static library file created.
1#include <stdio.h> 2#include "math.h" 3 4int main(void) 5{ 6 int x = 2; 7 int y = 4; 8 int result; 9 10 result = sum(x, y); 11 printf("Sum = %d\n", result); 12 13 result = sub(x, y); 14 printf("Sub = %d\n", result); 15 16 result = mul(x, y); 17 printf("Product = %d\n", result); 18 19 return 0; 20}
Step 5: Compile the application by linking the static library with gcc command.
gcc -o output app.c -L. -lmathfunc
Step 6: Executing the application by running the binary file.
$ ./output Sum = 6 Sub = -2 Product = 8
Step 1: Declare all the function which needs to be linked with the static library in the header file.
1int sum(int a,int b);
2int sub(int a,int b);
3int mul(int a,int b);
Step 2: Create source code file with function definitions for all the functions defined in header file.
1int sum (int a,int b)
2{
3 return a+b;
4}
1int sub (int a, int b)
2{
3 return a-b;
4}
1int mul (int a, int b)
2{
3 return a*b;
4}
Step 3: Creating Makefile with all the instructions required to generate the static library.
1CC = gcc
2CFLAGS = -Wall -Wextra -O2 -g
3RM = rm -f
4TARGET_LIB = libmathfunc.a
5APP = app
6
7SRCS = src/add.c src/sub.c src/mul.c
8OBJS = $(SRCS:.c=.o)
9
10.PHONY: all
11all: ${TARGET_LIB} ${APP}
12
13${APP}:
14 gcc -Wall -o app src/app.c -L./obj -lmathfunc -I./hdr
15 mv app bin
16
17$(TARGET_LIB): $(OBJS)
18 $(CC) -c src/add.c src/sub.c src/mul.c
19 ar -rc $(TARGET_LIB) src/add.o src/sub.o src/mul.o
20 mv *.o obj/
21 mv src/*.o obj/
22 mv src/*.d obj/
23 mv $(TARGET_LIB) obj/
24
25$(SRCS:.c=.d):%.d:%.c
26 $(CC) $(CFLAGS) -MM $< >$@
27
28include $(SRCS:.c=.d)
29
30.PHONY: clean
31clean:
32 rm -f obj/*
33 rm -f src/*.d
34
$ make
gcc -Wall -Wextra -O2 -g -MM src/mul.c >src/mul.d
gcc -Wall -Wextra -O2 -g -MM src/sub.c >src/sub.d
gcc -Wall -Wextra -O2 -g -MM src/add.c >src/add.d
gcc -Wall -Wextra -O2 -g -c -o src/add.o src/add.c
gcc -Wall -Wextra -O2 -g -c -o src/sub.o src/sub.c
gcc -Wall -Wextra -O2 -g -c -o src/mul.o src/mul.c
gcc -c src/add.c src/sub.c src/mul.c
ar -rc libmathfunc.a src/add.o src/sub.o src/mul.o
mv *.o obj/
mv src/*.o obj/
mv src/*.d obj/
mv libmathfunc.a obj/
gcc -Wall -o app src/app.c -L./obj -lmathfunc -I./hdr
mv app bin
$ ./app
Sum = 6
Sub = -2
Product = 8
Other topics of linux x86
Current Module
Previous Module
Next Module
Other Modules