C-Shared-Library without Makefile

Step 1.1 : Create a file sum.c

int sum(int a, int b)
{
   return (a + b);
}

Step 1.2 : Create a file sub.c

int sub(int a, int b)
{
   return (a - b);
}

Step 1.3 : Create a file prod.c

int prod(int a, int b)
{
   return (a * b);
}

Step 1.4 : Create a header file math.h

int sum(int a, int b);
int sub(int a, int b);
int prod(int a, int b);

Step 2 : Create a main application file app.c

#include <stdio.h>
#include "math.h"

int main(void)
{
    int x = 2;
    int y = 4;
    int result;

    result = sum(x, y);
    printf("Sum = %d\n", result);

    result = sub(x, y);
    printf("Sub = %d\n", result);

    result = prod(x, y);
    printf("Product = %d\n", result);

    return 0;
}

Step 3 : Create a shared library

$ gcc -c -fpic sum.c 
$ gcc -c -fpic sub.c
$ gcc -c -fpic prod.c

$ gcc -shared -o libmathfuns.so sum.o sub.o prod.o

$ nm libmathfuns.so 
0000000000004020 b completed.8060
                 w __cxa_finalize
0000000000001040 t deregister_tm_clones
00000000000010b0 t __do_global_dtors_aux
0000000000003e88 d __do_global_dtors_aux_fini_array_entry
0000000000004018 d __dso_handle
0000000000003e90 d _DYNAMIC
0000000000001140 t _fini
00000000000010f0 t frame_dummy
0000000000003e80 d __frame_dummy_init_array_entry
00000000000020f0 r __FRAME_END__
0000000000004000 d _GLOBAL_OFFSET_TABLE_
                 w __gmon_start__
0000000000002000 r __GNU_EH_FRAME_HDR
0000000000001000 t _init
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000001127 T prod
0000000000001070 t register_tm_clones
0000000000001111 T sub
00000000000010f9 T sum
0000000000004020 d __TMC_END__

$ export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH

Step 4 : Link shared library to application

$ gcc -L$PWD -Wall -o app app.c -lmathfuns

./app
Sum = 6
Sub = -2
Product = 8

C-Shared-Library with Makefile

Step 1.1 : Create a directory structure as below

$ tree library/
library/
├── hdr
│   └── math.h
├── Makefile
├── obj
└── src
    ├── app.c
    ├── prod.c
    ├── sub.c
    └── sum.c

Step 1.2 : Create a file sum.c under library/src folder

int sum(int a, int b)
{
   return (a + b);
}

Step 1.3 : Create a file sub.c under library/src folder

int sub(int a, int b)
{
   return (a - b);
}

Step 1.4 : Create a file prod.c under library/src folder

int prod(int a, int b)
{
   return (a * b);
}

Step 1.5 : Create a header file math.h under library/hdr folder

int sum(int a, int b);
int sub(int a, int b);
int prod(int a, int b);

Step 1.6 : Create a main application file app.c under library/src folder

#include <stdio.h>
#include "math.h"

int main(void)
{
    int x = 2;
    int y = 4;
    int result;

    result = sum(x, y);
    printf("Sum = %d\n", result);

    result = sub(x, y);
    printf("Sub = %d\n", result);

    result = prod(x, y);
    printf("Product = %d\n", result);

    return 0;
}

Step 2 : Create a file Makefile under library/ folder

# Makefile template for a shared library in C

CC = gcc  # C compiler
CFLAGS = -fPIC -Wall -Wextra -O2 -g  # C flags
LDFLAGS = -shared   # linking flags
RM = rm -f # rm command
TARGET_LIB = libmathfuns.so  # target lib
APP = app 

SRCS = src/sum.c src/sub.c src/prod.c  # source files
OBJS = $(SRCS:.c=.o)

.PHONY: all 
all: ${TARGET_LIB} ${APP}

${APP}:
    gcc -L$(PWD)/obj -Wall -o app src/app.c -lmathfuns -I./hdr
    mv app obj 

$(TARGET_LIB): $(OBJS)
    $(CC) ${LDFLAGS} -o $@ $^
    mv src/*.o obj/
    mv src/*.d obj/
    mv $(TARGET_LIB) obj/

$(SRCS:.c=.d):%.d:%.c
    $(CC) $(CFLAGS) -MM $< >$@

include $(SRCS:.c=.d)

.PHONY: clean
clean:
    rm -f obj/*
    rm -f src/*.d

Step 3 : Build library and application with make

$ cd library

$ make
gcc   -fPIC -Wall -Wextra -O2 -g   -MM src/prod.c >src/prod.d
gcc   -fPIC -Wall -Wextra -O2 -g   -MM src/sub.c >src/sub.d
gcc   -fPIC -Wall -Wextra -O2 -g   -MM src/sum.c >src/sum.d
gcc   -fPIC -Wall -Wextra -O2 -g     -c -o src/sum.o src/sum.c
gcc   -fPIC -Wall -Wextra -O2 -g     -c -o src/sub.o src/sub.c
gcc   -fPIC -Wall -Wextra -O2 -g     -c -o src/prod.o src/prod.c
gcc   -shared    -o libmathfuns.so src/sum.o src/sub.o src/prod.o
mv src/*.o obj/
mv src/*.d obj/
mv libmathfuns.so   obj/
gcc -L/home/test/Desktop/training/library/obj -Wall -o app src/app.c -lmathfuns -I./hdr
mv app obj

Step 4 : Check that .o, .so and app are copied to obj/ folder

$ ls obj/
app  libmathfuns.so  prod.d  prod.o  sub.d  sub.o  sum.d  sum.o

Step 5 : Run the program app which is linked to libmathfuns.so

$ cd obj
$ export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
$ ./app
Sum = 6
Sub = -2
Product = 8

Step 6 : Do clean build

$ cd library

$ make clean
gcc   -fPIC -Wall -Wextra -O2 -g   -MM src/prod.c >src/prod.d
gcc   -fPIC -Wall -Wextra -O2 -g   -MM src/sub.c >src/sub.d
gcc   -fPIC -Wall -Wextra -O2 -g   -MM src/sum.c >src/sum.d
rm -f obj/*
rm -f src/*.d

$ make
gcc   -fPIC -Wall -Wextra -O2 -g   -MM src/prod.c >src/prod.d
gcc   -fPIC -Wall -Wextra -O2 -g   -MM src/sub.c >src/sub.d
gcc   -fPIC -Wall -Wextra -O2 -g   -MM src/sum.c >src/sum.d
gcc   -fPIC -Wall -Wextra -O2 -g     -c -o src/sum.o src/sum.c
gcc   -fPIC -Wall -Wextra -O2 -g     -c -o src/sub.o src/sub.c
gcc   -fPIC -Wall -Wextra -O2 -g     -c -o src/prod.o src/prod.c
gcc   -shared    -o libmathfuns.so src/sum.o src/sub.o src/prod.o
mv src/*.o obj/
mv src/*.d obj/
mv libmathfuns.so   obj/
gcc -L/home/test/Desktop/training/library/obj -Wall -o app src/app.c -lmathfuns -I./hdr
mv app obj