November 24, 2017

Static library, Shared library and Dynamic library

❑ Static library : The library that is loaded when the program is built.
❍ *.a(Linux), *.lib(Windows)
❍ It is included in the executable file.
❍ It has relatively large executable file size and high memory usage.
❍ It can only be used in one process.
* e.g.
$ gcc -c file1.c
$ ar cr libnew.a file1.o
$ gcc -L/home/root/ -lnew -o main.out main.c
  → -l : This recognizes the rest of the library name except for the "lib" to the left and ".a(.so)" to the right.
  → -L : This indicates library path.

❑ Shared library : The library that is loaded when the program starts.
❍ *.so(Linux), *.sl(HP-UX) *.dll(Windows)
❍ It is not included in the executable file. Instead, there is information that makes the shared library available.
❍ It has relatively small executable file size and low memory usage.
❍ It can be used in several processes.
* e.g.
$ gcc -c main.c
$ gcc -c -fPIC file1.c
$ gcc -shared -fPIC -o libfile1.so file1.o
$ gcc -L/home/root/ -lfile -o main.out main.c

❑ Dynamic library : The library that is loaded when a library's function is called(Not the start time).
❍ It uses exclusive API(dlfcn.h). : dlopen(), dlsym(), dlerror(), dlclose()
* e.g.
$ gcc ~~ <Create shared library>
$ gcc -ldl -o main.out main.c


※ FYI.
- ldd : This is Linux commands to identify shared libraries used in a executable file.