Skip to content
Search

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

[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

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

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

Flag help

References


  1. Shared_Libraries_Versus_Loadable_Modules.pdf ↩

  2. Library_static_shared_dynamic.pdf ↩

  3. How_to_make_linux_library.pdf ↩

  4. Platform_Independent_Coding_-DLLs_and_SOs-_CodeProject.pdf ↩

  5. Using_static_and_shared_libraries_across_platforms.pdf ↩

  6. Lesstif.com_-_Shared_library_and_Linker_and_Loader.pdf ↩

author Zer0 sitemap