skip to content

For a C++ product built with GCC, how do you decide between static and shared linking for your own libraries, libstdc++ and the C library?

level: principalimportance: should knowfreq 40%

answer

  1. three layers, three answers
  2. deploy simplicity versus patching
  3. -static-libstdc++ middle ground
  4. plugins force shared
  5. one copy of each global

basics

~20 s

Decide per layer. Your own libraries are usually static for simple deployment and LTO, unless plugins or several executables share them. libstdc++ can be linked statically with -static-libstdc++. The C library usually stays shared because the system patches it.

solid answer

~50 s

There is no single answer, so split the question into layers. **Your own libraries**: static linking gives one self-contained binary, no loader search problems and whole-program LTO; shared libraries are needed for plugins loaded with `dlopen`, help when many executables share one large library, and allow patching a library without relinking, at the price of an ABI and visibility discipline. **libstdc++**: GCC's `-static-libstdc++` freezes the C++ runtime version without making the rest static, which lets one binary run on systems with an older libstdc++; the manual says modules that throw exceptions across shared-library boundaries should use the shared libgcc. **The C library**: usually shared, because the system vendor patches it and fully static glibc programs still need some shared modules at run time. Watch for the classic hazard of one static library linked into two shared objects, which gives two copies of its globals.

code

bash · 8 lines
bash
# own code static, C++ runtime frozen, C library from the system:
g++ -O2 main.o -L build -lcore -lgeo -static-libstdc++ -static-libgcc -o app

# fully static tool:
g++ -O2 -static main.o -L build -lcore -lgeo -o tool

# what the result still needs at run time:
readelf -d app | grep NEEDED

go deeper

for a junior

Know the basic difference: static libraries are copied into the program, shared libraries are loaded at run time.

for a middle

Explain the deployment, patching and optimization trade-offs, and what -static-libstdc++ and -static do.

for a senior

Recognise the duplicated-globals hazard, the exception rule for libgcc, and the glibc limits of fully static binaries.

for a principal

Set a per-layer linking policy for each product type, weighing security response, deployment, plugins and licensing, and document why.

## Frame the decision by layer "Static or shared?" mixes three separate choices with different trade-offs: 1. your own libraries; 2. the C++ runtime, libstdc++ and libgcc; 3. the C library and other system libraries. A product can, and usually should, answer them differently. ## Your own libraries | Factor | Static (`.a`) | Shared (`.so`) | |---|---|---| | Deployment | one file, nothing to find at run time | ship and locate every `.so` | | Optimization | LTO sees across library boundaries | calls cross a PLT, exported symbols block some inlining | | Patching | relink and redeploy the program | replace one file | | Plugins via `dlopen` | not possible for the plugin itself | required | | Many executables using one big library | duplicated in every binary | one copy on disk and in shared memory | | Interface discipline | internal | exported symbols and ABI become a contract | Static linking is the simpler default for applications and services. Shared libraries earn their cost when the product has plugins, a family of executables sharing a large core, or customers who must be able to replace a component. Choosing shared means adopting `-fvisibility=hidden`, explicit exports and sonames, and treating ABI compatibility, which the C++ tree covers, as a release rule. ## The C++ runtime GCC offers a middle ground. The manual describes `-static-libstdc++` as a way to **freeze the version of libstdc++** used by the program without going all the way to a fully static link. Typical use: a binary built on a new distribution must run on older ones whose system libstdc++ lacks newer symbols. Two caveats from the same manual: - When a program throws and catches exceptions **across shared-library boundaries**, each library and the application should use the **shared libgcc**. The `g++` driver adds `-shared-libgcc` automatically when building shared libraries and executables for that reason. - If several shared objects each link libstdc++ statically, each carries its own copy of the runtime, which is a recipe for subtle failures when C++ objects or exceptions pass between them. The middle ground is for executables, not for every library in a plugin system. ## The C library A fully static executable (`-static`) avoids every loader question and suits small tools and minimal containers. With glibc it has known limits: features such as name-service lookups and locale handling may still load shared modules at run time, and a static binary cannot take the system's security updates without a rebuild. Most products leave the C library shared and pin a **minimum** glibc version by building on the oldest supported system. `-static-pie` produces a static executable that can still be loaded at any address, without a dynamic linker. ## Hazards that cut across choices - **One static library inside two shared objects.** Each `.so` gets its own copy of the library's globals. With hidden visibility the copies stay separate, so a logger configured through one is unconfigured in the other. With default visibility the loader may bind both to one copy, whose constructor and destructor can then run twice. - **Security response time.** Every statically linked copy of a compression or TLS library is a separate rebuild when it gets a CVE. - **Licensing.** Some licenses attach different obligations to static and dynamic linking; involve whoever owns compliance. - **Startup cost.** Many shared libraries with large export tables add relocation work at every launch. ## A decision outline - Desktop application or game: static for your own code, bundled `.so` files with `$ORIGIN` rpaths for third-party pieces that must stay replaceable, `-static-libstdc++` if older distributions must be supported. - Server in a controlled container image: dynamic against the image's libraries, rebuilt with the image. - Command-line tool distributed widely: as static as the C library allows, or `-static-libstdc++` with a pinned minimum glibc. - Plugin host: shared core, strict exports, one shared copy of the C++ runtime for every module.

  • Why can -static-libstdc++ be dangerous in a plugin architecture?
    Each module that links libstdc++ statically carries its own copy of the C++ runtime and its internal state. Passing exceptions or standard library objects between modules then depends on several copies agreeing, which fails in ways that are hard to diagnose. A plugin host should share one dynamic C++ runtime, and the shared libgcc, across all modules.
  • What is the minimum-glibc strategy for distributing a dynamic Linux binary?
    Build on the oldest distribution you support, because a binary depends on the versioned glibc symbols it was linked against and runs on that glibc or newer. Combined with `-static-libstdc++`, the binary then avoids both a too-new glibc and a too-new libstdc++ requirement.

  • Static linking is always faster, so it is always the right choice.
  • Shared libraries need no ABI discipline because the loader resolves everything.
  • -static-libstdc++ also makes the C library static.
  • Linking one static library into two shared libraries gives them a single shared copy of its globals.
  • A fully static binary picks up system security fixes automatically.