Diamond Dependencies & Conflicts
Your app uses net and db. Both use fmt. Draw it and you get a diamond:
app
/ \
net db
\ /
fmt net wants fmt ^1.0, db wants fmt ^2.0If net and db accept overlapping versions of fmt, the resolver picks one and everyone is happy. If they don't (^1.0 and ^2.0 share no version), you have a diamond conflict. What happens next depends entirely on the ecosystem, and in C and C++ the answer can be a program that links fine and then quietly calls the wrong code.
How ecosystems handle a diamond
| Strategy | Who | Result for fmt ^1 + fmt ^2 |
|---|---|---|
| One version per package, fail if impossible | pip/uv/Poetry, Conan (by default), vcpkg, Maven/Gradle (with their own picking rules), Go (one per major) | an error you must fix (or Maven/Gradle silently pick one) |
| Several versions side by side, each consumer gets its own | npm (nested node_modules), Cargo (for different semver-incompatible versions) |
both are installed; works if the two never exchange fmt types |
| Whatever the linker does | C and C++ without a package manager | link error, or a silent wrong choice at run time |
Go is a hybrid: fmt/v1 and fmt/v2 are different module paths, so both can coexist; within one major version it picks a single version for the whole build. Cargo allows fmt 1.x and fmt 2.x in one binary, but if net returns a fmt 1 type and db expects a fmt 2 type, compilation fails with a confusing mismatch between two types that have the same name (rustc adds a note that two different versions of the crate are in use).
Model: single-version vs nested
The model resolves the same diamond with both strategies. The registry has four fmt releases:
const versions = ["1.0.0", "1.4.0", "2.0.0", "2.1.0"];
const wants = { net: { fmt: [">=1.0.0", "<2.0.0"] }, db: { fmt: [">=2.0.0", "<3.0.0"] }, log: { fmt: [">=1.2.0", "<2.0.0"] } };
const num = (v) => v.split(".").map(Number).reduce((a, x) => a * 1000 + x);
const ok = (v, [lo, hi]) => num(v) >= num(lo.slice(2)) && num(v) < num(hi.slice(1));
const best = (ranges) => versions.filter((v) => ranges.every((r) => ok(v, r))).sort((a, b) => num(b) - num(a))[0];
function singleVersion(consumers) {
const ranges = consumers.map((c) => wants[c].fmt);
const v = best(ranges);
return v ? `fmt ${v} for ${consumers.join(", ")}`
: `CONFLICT: no fmt version satisfies ${consumers.map((c) => `${c} (${wants[c].fmt.join(" ")})`).join(" and ")}`;
}
function nested(consumers) {
const groups = {};
for (const c of consumers) (groups[best([wants[c].fmt])] ??= []).push(c);
return Object.entries(groups).map(([v, cs]) => `fmt ${v} for ${cs.join(", ")}`).join("; ");
}
for (const consumers of [["net", "log"], ["net", "db"], ["net", "db", "log"]]) {
console.log(`[${consumers}]`);
console.log(" single version:", singleVersion(consumers));
console.log(" nested :", nested(consumers));
}[net,log]
single version: fmt 1.4.0 for net, log
nested : fmt 1.4.0 for net, log
[net,db]
single version: CONFLICT: no fmt version satisfies net (>=1.0.0 <2.0.0) and db (>=2.0.0 <3.0.0)
nested : fmt 1.4.0 for net; fmt 2.1.0 for db
[net,db,log]
single version: CONFLICT: no fmt version satisfies net (>=1.0.0 <2.0.0) and db (>=2.0.0 <3.0.0) and log (>=1.2.0 <2.0.0)
nested : fmt 1.4.0 for net, log; fmt 2.1.0 for db
Single-version resolution finds the overlap (fmt 1.4.0 for net and log) and fails loudly when there is none. Nested installation always "works", at the price of two copies of fmt on disk and in memory, and possible type mismatches across the boundary.
The C version: one symbol namespace
C has no packages and no versions in symbol names. On Linux, all shared libraries in a process share one global symbol namespace by default: when two loaded libraries define fmt_version, every caller gets the one found first in load order. Here libnet.so was built against libfmt.so.1 and libdb.so against libfmt.so.2; both libraries load, and both callers get the same function:
mkdir -p diamond/v1 diamond/v2 && cd diamond
printf 'const char *fmt_version(void) { return "fmt 1.0"; }\n' > v1/fmt.c
printf 'const char *fmt_version(void) { return "fmt 2.0"; }\n' > v2/fmt.c
gcc -fPIC -shared -Wl,-soname,libfmt.so.1 v1/fmt.c -o v1/libfmt.so.1 && ln -s libfmt.so.1 v1/libfmt.so
gcc -fPIC -shared -Wl,-soname,libfmt.so.2 v2/fmt.c -o v2/libfmt.so.2 && ln -s libfmt.so.2 v2/libfmt.so
printf 'const char *fmt_version(void);\nconst char *net_uses(void) { return fmt_version(); }\n' > net.c
printf 'const char *fmt_version(void);\nconst char *db_uses(void) { return fmt_version(); }\n' > db.c
gcc -fPIC -shared net.c -Lv1 -lfmt -Wl,-rpath,'$ORIGIN/v1' -o libnet.so
gcc -fPIC -shared db.c -Lv2 -lfmt -Wl,-rpath,'$ORIGIN/v2' -o libdb.so
cat > app.c <<'SRC'
#include <stdio.h>
const char *net_uses(void);
const char *db_uses(void);
int main(void) {
printf("net (built with fmt 1) calls: %s\n", net_uses());
printf("db (built with fmt 2) calls: %s\n", db_uses());
return 0;
}
SRC
gcc app.c -L. -lnet -ldb -Wl,-rpath,'$ORIGIN' -o app && ./app
echo "--- same program, libraries listed the other way round"
gcc app.c -L. -ldb -lnet -Wl,-rpath,'$ORIGIN' -o app && ./appnet (built with fmt 1) calls: fmt 1.0
db (built with fmt 2) calls: fmt 1.0
--- same program, libraries listed the other way round
net (built with fmt 1) calls: fmt 2.0
db (built with fmt 2) calls: fmt 2.0
Both versions of libfmt were loaded, yet only one fmt_version is ever called, and which one depends on link order. No warning at build time or run time. If version 2 had changed what the function does (or its arguments), one of the two libraries would now be running with the wrong code: a crash, or worse, wrong results.
With static libraries the same diamond is at least loud when both definitions end up in the link:
mkdir -p sdiamond && cd sdiamond
printf 'const char *fmt_version(void) { return "fmt 1.0"; }\n' > fmt1.c
printf 'const char *fmt_version(void) { return "fmt 2.0"; }\n' > fmt2.c
printf 'const char *fmt_version(void);\nconst char *net_uses(void) { return fmt_version(); }\n' > net.c
printf 'const char *fmt_version(void);\nconst char *db_uses(void) { return fmt_version(); }\n' > db.c
printf '#include <stdio.h>\nconst char *net_uses(void); const char *db_uses(void);\nint main(void) { printf("%%s / %%s\\n", net_uses(), db_uses()); return 0; }\n' > app.c
gcc -c fmt1.c fmt2.c net.c db.c app.c
echo "--- objects: multiple definition"
gcc app.o net.o db.o fmt1.o fmt2.o -o app
echo "--- archives: first one silently wins"
ar rcs libfmt1.a fmt1.o && ar rcs libfmt2.a fmt2.o
gcc app.o net.o db.o -L. -lfmt1 -lfmt2 -o app && ./app--- objects: multiple definition
/usr/bin/ld: fmt2.o: in function `fmt_version':
fmt2.c:(.text+0x0): multiple definition of `fmt_version'; fmt1.o:fmt1.c:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
--- archives: first one silently wins
fmt 1.0 / fmt 1.0
With archives, the linker takes fmt_version from the first one that resolves it and never looks inside the second: silent again.
Fixes in C and C++
- Have one version. The real fix, and the reason C/C++ package managers (Conan, vcpkg) resolve one version per package for the whole build graph.
- Symbol versioning. glibc-style version scripts tag each exported symbol (
fmt_version@FMT_1,fmt_version@FMT_2), and each consumer records the version it linked against. The loader then binds each caller to the right library:
mkdir -p diamond/v1 diamond/v2 && cd diamond
printf 'const char *fmt_version(void) { return "fmt 1.0"; }\n' > v1/fmt.c
printf 'const char *fmt_version(void) { return "fmt 2.0"; }\n' > v2/fmt.c
echo 'FMT_1 { global: fmt_*; local: *; };' > v1/fmt.map
echo 'FMT_2 { global: fmt_*; local: *; };' > v2/fmt.map
gcc -fPIC -shared -Wl,-soname,libfmt.so.1 -Wl,--version-script=v1/fmt.map v1/fmt.c -o v1/libfmt.so.1 && ln -s libfmt.so.1 v1/libfmt.so
gcc -fPIC -shared -Wl,-soname,libfmt.so.2 -Wl,--version-script=v2/fmt.map v2/fmt.c -o v2/libfmt.so.2 && ln -s libfmt.so.2 v2/libfmt.so
printf 'const char *fmt_version(void);\nconst char *net_uses(void) { return fmt_version(); }\n' > net.c
printf 'const char *fmt_version(void);\nconst char *db_uses(void) { return fmt_version(); }\n' > db.c
gcc -fPIC -shared net.c -Lv1 -lfmt -Wl,-rpath,'$ORIGIN/v1' -o libnet.so
gcc -fPIC -shared db.c -Lv2 -lfmt -Wl,-rpath,'$ORIGIN/v2' -o libdb.so
printf '#include <stdio.h>\nconst char *net_uses(void); const char *db_uses(void);\nint main(void) { printf("net: %%s\\ndb : %%s\\n", net_uses(), db_uses()); return 0; }\n' > app.c
gcc app.c -L. -lnet -ldb -Wl,-rpath,'$ORIGIN' -o app && ./app
nm -D libnet.so libdb.so | grep fmt_versionnet: fmt 1.0
db : fmt 2.0
U fmt_version@FMT_1
U fmt_version@FMT_2
- Hide private copies. A library that bundles its own copy of a dependency should build it with
-fvisibility=hidden(or link it statically with-Wl,--exclude-libs,ALL) so the copy's symbols never enter the global namespace. - Rename (namespace prefix, or a C++
inline namespace v2, which changes the mangled names): this is how libraries such as fmt and Abseil let different versions coexist safely.
Test yourself
net needs fmt ^1.0 and db needs fmt ^2.0. What does a single-version resolver (pip, Conan) do?
- Installs both
- Reports a conflict, because no version satisfies both ranges
- Picks 2.0 silently
- Picks 1.0 silently
In the shared-library session, why did both net and db call the same fmt_version?
- Only one library was loaded
- All loaded libraries share one global symbol namespace; the first definition in load order wins
- The compiler merged them
dbwas not linked
How did the version script fix the diamond?
- It removed one library
- Each symbol gets a version tag (
@FMT_1,@FMT_2) that consumers record, so the loader binds each caller to the right definition - It made the libraries static
- It renamed the functions