Library
lastmod 2021-11-05created 2013-12-2953 edits
A library is a collection of classes or subroutines used when building software.
Static library
A static library is a program module referenced when the compiler compiles source files. That is, a static library (statically-linked library) is a set of routines, external functions, and variables that the compiler, linker, binder, etc. copy into the target application to produce an executable that can run independently of the object files. Like .LIB files on Windows, it has the .a extension.
How to use
[root@localhost ~]$ gcc -c func1.c func2.c
[root@localhost ~]$ ls
func1.c func1.o func2.c func2.o program.c
[root@localhost ~]$ ar crv libfoo.a func1.o func2.o
a - func1.o
a - func2.o
[root@localhost ~]$ gcc program.c -o program libfoo.a
[root@localhost ~]$ ls
func1.c func1.o func2.c func2.o lib.h program program.c libfoo.a
[root@localhost ~]$ ./program
Shared Library
Shared library.
How to use
- [Recommended] Static Library & Shared Library
[root@localhost ~]$ gcc -fPIC -c *.c
[root@localhost ~]$ gcc --shared -Wl,-soname,libfoo.so -o libfoo.so func1.o func2.o
[root@localhost ~]$ nm -D libfoo.so
[root@localhost ~]$ sudo ldconfig -n .
[root@localhost ~]$ gcc -o program program.o -L. -lfoo
[root@localhost ~]$ ldd program
OR:
[root@localhost ~]$ gcc -fPIC -c *.c
[root@localhost ~]$ gcc -shared -Wl,-soname,libfoo.so.1 -o libfoo.so.1.0 *.o
[root@localhost ~]$ ln -s libfoo.so.1.0 libfoo.so.1
[root@localhost ~]$ ln -s libfoo.so.1 libfoo.so
[root@localhost ~]$ LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH ; export LD_LIBRARY_PATH
[root@localhost ~]$ su
[root@localhost ~]# cp libfoo.so.1.0 /usr/local/lib
[root@localhost ~]# /sbin/ldconfig
[root@localhost ~]# ( cd /usr/local/lib ; ln -s libfoo.so.1 libfoo.so )
Dynamic Library
- Dynamic-link library
- DLL:Example:Windows - How to load a DLL dynamically on Windows.
A dynamic library is a program module loaded when it is needed while the program is running. On Windows it usually has the .dll extension, and on Linux usually the .so extension.
How to check
Dependencies can be checked with the tools below.
Relocation
Load-time relocation of shared libraries, notes on load-time relocation of shared libraries.
Dynamic vs Shared
Dynamic libraries are libraries loaded at run-time. Shared libraries, or shared objects, are dynamic libraries designed so that a single copy can be shared among running processes.
- Implicit linking
- Implicit linking loads all required DLLs into memory before the program runs. As a result, the time until execution may be longer.
- Explicit linking
- Explicit linking can load DLLs one at a time when they are needed, so the time until execution is correspondingly shorter, and the time spent loading DLLs can be spread out.
- There are Win32 Api functions such as LoadLibrary, GetProcAddress, and FreeLibrary.
MSVC Dynamic library
For sample code, see here.
Mac OSX Shared libraries
For GCC
- KLDP - 4. Dynamically Loaded (DL) Libraries
- [Recommended] Making a Library (static, shared, dynamic) 2
- How to make a linux library (static, shared, dynamic) 3
Troubleshooting
Unsafe use of relative rpath
On MacOSX, the following error message may be printed.
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/cv2.so, 2): Library not loaded: lib/libopencv_shape.3.0.dylib
Referenced from: /usr/local/lib/python2.7/site-packages/cv2.so
Reason: unsafe use of relative rpath lib/libopencv_shape.3.0.dylib in /usr/local/lib/python2.7/site-packages/cv2.so with restricted binary
In this case, use install_name_tool to change the library's relative path to an absolute path.
See also
Favorite site
- Wikipedia (en) Description of Library
- How do you make a library on Linux?
- [Recommended] Joinc: Making a library
- Joinc: Using a library written in C++
- Platform Independent Coding - DLLs and SOs 4
- C++ Cross Platform Dynamic Libraries; Linux and Windows
- Making a linux library (static, shared, dynamic)
- Make-based build system (9): Shared library support
- 2.3 Writing and Using Libraries
- [Recommended] Using static and shared libraries across platforms 5
- [Recommended] Understanding dynamic libraries (shared library) and the Linker/Loader 6
Flag help
- Program Library HOWTO (about rdynamic)
- gcc -rdynamic option?
- Error attaching a shared object with dlopen on Solaris, gcc (about
--export-dynamic)