C-Static-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 static library

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

$ ar -rc libmathfuns.a sum.o sub.o prod.o

$ nm libmathfuns.a
sum.o:
0000000000000000 T sum

sub.o:
0000000000000000 T sub

prod.o:
0000000000000000 T prod

Step 4 : Link static library to application

Explanation

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

$ ./app
Sum = 6
Sub = -2
Product = 8
  • -L says “look in directory for library filesâ€

  • . (the dot after ‘L’) represents the current working directory

  • -l says “link with this library fileâ€

  • mathfuns is the name of our library. Note that we omitted the “lib†prefix and “.a†extension.

  • The linker attaches these parts back to the name of the library to create a name of a file to look for.

  • -o app says “name the executable file appâ€

C-Static-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 static library in C

CC = gcc  # C compiler
CFLAGS = -Wall -Wextra -O2 -g  # C flags
RM = rm -f # rm command
TARGET_LIB = libmathfuns.a  # 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) -c src/sum.c src/sub.c src/prod.c
    ar -rc $(TARGET_LIB) src/sum.o src/sub.o src/prod.o
    mv *.o obj/
    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   -Wall -Wextra -O2 -g   -MM src/prod.c >src/prod.d
gcc   -Wall -Wextra -O2 -g   -MM src/sub.c >src/sub.d
gcc   -Wall -Wextra -O2 -g   -MM src/sum.c >src/sum.d
gcc   -Wall -Wextra -O2 -g     -c -o src/sum.o src/sum.c
gcc   -Wall -Wextra -O2 -g     -c -o src/sub.o src/sub.c
gcc   -Wall -Wextra -O2 -g     -c -o src/prod.o src/prod.c
gcc   -c src/sum.c src/sub.c src/prod.c
ar -rc libmathfuns.a   src/sum.o src/sub.o src/prod.o
mv *.o obj/
mv src/*.o obj/
mv src/*.d obj/
mv libmathfuns.a   obj/
gcc -L/home/test/Desktop/training/static_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.a  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

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

Step 6 : Do clean build

$ cd library

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

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