Shared Libraries (Preview)#
Different Flavors Of Library#
STATICSimple bag of objects
At executable link time: linker copies needed object into executable
Pro
Executable is standalone (“statically linked against that library”)
Con
Library update (bug fix?) requires rebuild of all executables that depend on it
Large executables
SHAREDAt executable link time: linker just verifies all is there, but code remains in library
Loaded at executable startup time
⟶ Loader (usually
/lib64/ld-linux-x86-64.so.2)Pro
Small executable
Library update is just a matter of copying the new library version
“More professional”
Con
Rather complex
Requires understanding of library versioning
Not quite easy during development; roundtrips are longer and more error prone
MODULEPlugin that is loaded explicitly by the program, at an arbitrary time in its lifetime
Building Shared Libraries#
In the
CMakeLists.txt, usingSHAREDtag⟶ Library is always built as a shared library
add_library(hello SHARED hello.c hello-name.c) # <-- shared library onlySwitchable at build/config time
Convenient during development
cmake -DBUILD_SHARED_LIBS=ON|OFF ...add_library(hello hello.c hello-name.c) # <-- defaults to STATIC$ cmake -DBUILD_SHARED_LIBS=ON ...
More About Loading#
Program interpreter ⟶ hash bang (e.g.
#!/bin/sh) in scriptsFor executables:
/lib64/ld-linux-x86-64.so.2Library
SONAMEandNEEDEDtags ⟶ look in/usr/lib64/$ rpm -qf /usr/lib64/libboost_regex.so.1.76.0 $ sudo dnf install boost-devel
A-ha: development versions
$ ls -l /usr/lib64/libboost_regex* lrwxrwxrwx. 1 root root 24 Jul 14 2022 /usr/lib64/libboost_regex.so -> libboost_regex.so.1.76.0 -rwxr-xr-x. 1 root root 289264 Jul 14 2022 /usr/lib64/libboost_regex.so.1.76.0
Temporarily add
set_property(TARGET hello PROPERTY VERSION "1")``and modify to
project(Demo VERSION "1.0.0") set_property(TARGET hello PROPERTY VERSION ${PROJECT_VERSION})
Discuss ABI versions ⟶ rarely project versions can be used as ABI versions
⟶ can of worms: installation/deployment