List:
mkdir library
and cd library
mkdir lib include
vim include/mylib.h
#ifndef __MYLIB_H__ #define __MYLIB_H__ int qpow(int, int); #endif /* __MYLIB_H__ */
vim mylib.c
/****************************************************************************** * File: mylib.c * * Author: Gsharp * Created: 05/15/21 * Description: library mylib implements *****************************************************************************/ #include "mylib.h" int qpow(int a, int n) { int res = 1; if (a < 0 || n < 0) { return -1; } while (n) { if (n & 1) res *= a; a *= a; n >>= 1; } return res; }
#include <stdio.h> #include "mylib.h" int main(int argc, char *argv[]) { printf("%d ^ %d = %d", 3, 10, qpow(3, 10)); return 0; }
gcc -g -Wall -Werror -fPIC -c lib/mylib.c -I./include
you will get mylib.oar rcs lib/libmylib.a mylib.o
gcc test.c -static -I include/ -L lib/ -lmylib -o test
gcc -g -Wall -Werror -Wextra -fPIC -shared -pedantic mylib.c -o lib/libmylib.so
export LD_LIBRARY_PATH=./lib:$LD_LIBRARY_PATH
gcc test.c -I inlude/ -L lib/ -lmylib -o test