Adding Shared Library
In this section, you are going to learn
What is shared library ?
How to create shared library ?
How to link shared library ?
Let us answer few basic questions about shared 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 Shared Library ?
See Answer
Collection of compiled object code that is linked to the source code only at the runtime.
How to link the object files to generate the shared library ?
See Answer
object files should be generated with -fPIC option in gcc command
-shared option in gcc is used to generate the static library.
gcc -fPIC file.c file.o
gcc -shared -o mysharedlib.so file.o
How to link the shared library with the source code ?
See Answer
set LD_LIBRARY_PATH to your shared library path and run the executable binary.
export LD_LIBRARY_PATH=<YOUR_LIBRARY_PATH>:$LD_LIBRARY_PATH
./a.out
What “nm” command does ?
See Answer
nm command is used to display information about symbol in objects file or binaries.
$ nm -D libmathfunc.so
w __cxa_finalize
w __gmon_start__
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
0000000000001127 T mul
0000000000001111 T sub
00000000000010f9 T sum
-D option is used to display dynamic symbols.
What objdump command does ?
See Answer
objdump is used to provide detailed information about object files and libraries.
$ objdump -T libmathfunc.so
libmathfunc.so: file format elf64-x86-64
DYNAMIC SYMBOL TABLE:
0000000000000000 w D *UND* 0000000000000000 __cxa_finalize
0000000000000000 w D *UND* 0000000000000000 _ITM_registerTMCloneTable
0000000000000000 w D *UND* 0000000000000000 _ITM_deregisterTMCloneTable
0000000000000000 w D *UND* 0000000000000000 __gmon_start__
0000000000001127 g DF .text 0000000000000017 mul
00000000000010f9 g DF .text 0000000000000018 sum
0000000000001111 g DF .text 0000000000000016 sub
-T option is used to display dynamic symbol table.
What readelf command does ?
See Answer
readelf is used for displaying information about ELF (Executable and Linkable Format) format binaries.
What file command does ?
See Answer
file is used for providing basic information about the file including whether it is a shared library.
$ file libmathfunc.so
libmathfunc.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=39a0f0d0a3ff4477eb4fc706a0b85ad38f4501f8, not stripped
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 Shared Library.
$ gcc -c -fPIC add.c $ gcc -c -fPIC sub.c $ gcc -c -fPIC mul.c $ gcc -shared -o libmathfunc.so 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.
export LD_LIBRARY_PATH=$(PWD):$LD_LIBRARY_PATH 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 = -fPIC -Wall -Wextra -O2 -g
3LDFLAGS = -shared
4RM = rm -f
5TARGET_LIB = libmathfunc.so
6APP = app
7
8SRCS = src/add.c src/sub.c src/mul.c
9OBJS = $(SRCS:.c=.o)
10
11.PHONY: all
12all: ${TARGET_LIB} ${APP}
13
14${APP}:
15 gcc -L$(PWD)/obj -Wall -o app src/app.c -lmathfunc -I./hdr
16 mv app obj
17
18$(TARGET_LIB): $(OBJS)
19 $(CC) ${LDFLAGS} -o $'@' $^
20 mv src/*.o obj/
21 mv src/*.d obj/
22 mv $(TARGET_LIB) obj/
23
24$(SRCS:.c=.d):%.d:%.c
25 $(CC) $(CFLAGS) -MM $< >$@
26
27include $(SRCS:.c=.d)
28
29.PHONY: clean
30clean:
31 rm -f obj/*
32 rm -f src/*.d
$ 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.so obj/
gcc -Wall -o app src/app.c -L./obj -lmathfunc -I./hdr
mv app obj
$ ./app
Sum = 6
Sub = -2
Product = 8
Other topics of linux x86
Current Module
Previous Module
Next Module
Other Modules