Shared Libraries: Versions & Visibility
Static libraries are copied into the program and forgotten. Shared libraries are files the program loads at run time, so two extra questions appear: which version of the library is the program compatible with, and which symbols does the library expose? CMake handles both with target properties.
VERSION and SOVERSION
On Linux a shared library has three names:
| Name | Example | Who uses it |
|---|---|---|
| real name | libgreet.so.1.4.0 |
the actual file |
| soname | libgreet.so.1 |
recorded inside programs at link time; the loader looks for this name |
| linker name | libgreet.so |
used by -lgreet when you build against it |
The soname changes only when the ABI breaks (major version). CMake creates all three when you set VERSION and SOVERSION:
mkdir -p greet && cd greet
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Greet VERSION 1.4.0 LANGUAGES CXX)
add_library(greet SHARED greet.cpp)
set_target_properties(greet PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR})
add_executable(app main.cpp)
target_link_libraries(app PRIVATE greet)
CM
echo 'const char* greet() { return "hi"; }' > greet.cpp
cat > main.cpp <<'SRC'
#include <cstdio>
const char* greet();
int main() { std::puts(greet()); }
SRC
cmake -S . -B build -G Ninja --log-level=NOTICE > /dev/null
cmake --build build
cd build
ls -l libgreet* | awk '{print $9, $10, $11}'
readelf -d libgreet.so.1.4.0 | grep SONAME
readelf -d app | grep -E 'NEEDED|RUNPATH'
./app[1/5] Building CXX object CMakeFiles/greet.dir/greet.cpp.o
[2/5] Linking CXX shared library libgreet.so.1.4.0
[3/5] Creating library symlink libgreet.so.1 libgreet.so
[4/5] Building CXX object CMakeFiles/app.dir/main.cpp.o
[5/5] Linking CXX executable app
libgreet.so -> libgreet.so.1
libgreet.so.1 -> libgreet.so.1.4.0
libgreet.so.1.4.0
0x000000000000000e (SONAME) Library soname: [libgreet.so.1]
0x0000000000000001 (NEEDED) Shared library: [libgreet.so.1]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
0x000000000000001d (RUNPATH) Library runpath: [/home/learner/greet/build]
hi
Two things to notice in the readelf output:
appneedslibgreet.so.1(the soname), notlibgreet.so.1.4.0. A bug-fix release1.4.1can replace the file and the symlink without relinking anything.apphas aRUNPATHpointing at the build directory, so it runs straight from the build tree. CMake adds that automatically and removes it on install; the RPATH lesson shows what to put there instead.
Symbol visibility
By default GCC and Clang export every non-static function from a shared library. That makes the library slower to load, lets users depend on internals, and differs from Windows, where nothing is exported unless marked __declspec(dllexport). The portable approach: hide everything by default and mark the public API.
CMake has properties for the first part and the GenerateExportHeader module for the second:
mkdir -p vis && cd vis
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Vis LANGUAGES CXX)
include(GenerateExportHeader)
add_library(shapes SHARED shapes.cpp)
set_target_properties(shapes PROPERTIES
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON)
generate_export_header(shapes) # writes shapes_export.h into the build dir
target_include_directories(shapes PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
CM
cat > shapes.cpp <<'SRC'
#include "shapes_export.h"
double helper_square(double x) { return x * x; } // internal
SHAPES_EXPORT double circle_area(double r) { return 3.14159 * helper_square(r); }
SRC
cmake -S . -B build -G Ninja --log-level=NOTICE > /dev/null
cmake --build build > /dev/null
nm -DC --defined-only build/libshapes.so | grep -E 'circle|helper'
sed -n '/ifdef shapes_EXPORTS/,/endif/p' build/shapes_export.h0000000000001111 T circle_area(double)
# ifdef shapes_EXPORTS
/* We are building this library */
# define SHAPES_EXPORT __attribute__((visibility("default")))
# else
/* We are using this library */
# define SHAPES_EXPORT __attribute__((visibility("default")))
# endif
Only circle_area is exported; helper_square stays internal. The generated header defines SHAPES_EXPORT as __attribute__((visibility("default"))) here and as __declspec(dllexport)/__declspec(dllimport) on Windows, so the same source works everywhere. It also sets SHAPES_EXPORT correctly whether the library is being built shared or static (shapes_EXPORTS is defined only while building the shared library).
Tip: On Windows,
CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ONexports everything without annotations, the quick way to port a Unix library. Hidden-by-default plus an export macro is the cleaner long-term setup.
Position-independent code
Code in a shared library must be compiled with -fPIC. CMake adds it automatically for SHARED and MODULE libraries. If a static library will be linked into a shared library, it needs PIC too: set POSITION_INDEPENDENT_CODE ON on that target (or CMAKE_POSITION_INDEPENDENT_CODE ON globally). Without it the link fails with relocation R_X86_64_PC32 … can not be used when making a shared object; recompile with -fPIC.
Test yourself
What does a program record when it links against libgreet.so with SOVERSION 1?
libgreet.solibgreet.so.1(the soname)libgreet.so.1.4.0- The absolute path
What does CXX_VISIBILITY_PRESET hidden do?
- Hides the library from find_package
- Compiles with
-fvisibility=hidden, so only symbols marked for export are visible - Strips debug info
- Makes the library static
A static library is linked into a shared library and the link fails with 'recompile with -fPIC'. The fix?
- Make the static library SHARED
- Set
POSITION_INDEPENDENT_CODE ONon the static library - Add
-sharedto the executable - Use BUILD_SHARED_LIBS=OFF