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 ?

What are the types of library ?

What is Shared Library ?

How to link the object files to generate the shared library ?

How to link the shared library with the source code ?

What “nm” command does ?

What objdump command does ?

What readelf command does ?

What file command does ?

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