C++

DSO Missing From Command Line Fix

Your program calls a function from library B itself, so library B belongs on your link line itself. That A already depends on B covers A's own calls, not yours: name every library whose functions the program calls directly, and this error goes away.

Written November 26, 2026 · Tested with GCC 13.3.0 · 8 min read

By the end of this guide
  • The setup: library A depends on library B
  • The error: the program calls B directly but links only A
  • The fix: name B on the link line
  • The same error with --as-needed spelled out

Written November 26, 2026

Tested with GCC 13.3.0

What this guide is

This error is the linker drawing a line between two kinds of use. A library that another library depends on is searched to resolve that library's own references. A function your own program calls directly is a different claim: the library that defines it must be named on your link line, in person. When the program calls a function from library B directly but the link line names only library A, which itself depends on B, the modern GNU linker refuses to let A's dependency silently cover the program's direct call, and it says so in two lines: an undefined reference to B's symbol, and DSO missing from command line naming B's file.

The fix follows from that distinction, and it is a link-line fix, not a code fix: add B to the command line next to A. Nothing in the program or in either library is wrong, and neither library needs to be rebuilt. The sections below build the smallest chain that produces the error, a libb.so that defines one function, a liba.so that calls it, and a program that calls both, reproduce the exact message, and then apply the fix.

Two nearby shapes explain most of the confusion this error causes in larger builds, and both are shown with real output. The legacy --copy-dt-needed-entries option restores the older behavior of following A's dependency to resolve the program's symbols, but only for libraries named after the option on the command line, so the same option placed after -la changes nothing. And a program that calls only A's function, never B's, links with A alone: the transitive dependency is enough when the program's own references stop at A.

The examples here use GCC 13.3.0 and GNU ld, with C++20 sources. Each failing link prints its diagnostic to standard error and exits with status 1; each fixed link is followed by running the program, and its output is shown exactly as printed.

The setup: library A depends on library B

b.cpp defines one function in what becomes library B:

C++
int b_answer() {
    return 42;
}

a.cpp calls that function and adds one, and it becomes library A:

C++
int b_answer();

int a_answer() {
    return b_answer() + 1;
}

Building both as shared libraries, with A's link naming B, gives A a recorded dependency on B. readelf shows it as a DT_NEEDED entry inside liba.so:

Terminal
$ g++ -std=c++20 -fPIC -shared -o libb.so b.cpp
$ g++ -std=c++20 -fPIC -shared -o liba.so a.cpp -L. -lb -Wl,-rpath,.
$ readelf -d liba.so | grep NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libb.so]

That entry is real, and it is what makes the error surprising the first time: the dependency is recorded, B will be loaded when the program runs, and A's own call to b_answer() resolves through it. What the entry does not do, under the linker's default behavior, is stand in for the program naming B itself.

The program calls both functions, a_answer() from A and b_answer() from B:

C++
#include <iostream>

int a_answer();
int b_answer();

int main() {
    std::cout << a_answer() << ' ' << b_answer() << '\n';
    return 0;
}

Compiled and linked with only A on the link line, the link fails:

Terminal
$ g++ -std=c++20 -c main.cpp -o main.o
$ g++ main.o -o main -L. -la -Wl,-rpath,.
/usr/bin/ld: main.o: undefined reference to symbol '_Z8b_answerv'
/usr/bin/ld: ./libb.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

Read the two linker lines as a pair. The first names the symbol the program needs, _Z8b_answerv, the mangled name of b_answer(). The second names the file that defines it, ./libb.so, and gives the reason it was not used: the DSO, the dynamic shared object, is missing from the command line. The linker found B, through A's dependency; it is refusing to use it for the program's own reference. collect2 then reports that ld returned exit status 1.

Add -lb next to -la and repeat the link. Nothing else changes, same object file, same libraries:

Terminal
$ g++ main.o -o main -L. -la -lb -Wl,-rpath,.
$ ./main
43 42

The link prints nothing, and the program prints 43 42: a_answer(), which is B's 42 plus one, and b_answer() itself. The order of the two libraries follows the usual rule, a library is named after the code that needs it, and here both are named after main.o, with A first because the program's first reference is to A. In a build system, the same fix is one entry in the target's link list: the library moves from being only A's dependency to also being a direct dependency of the program, which is what it always was in the source.

The same error with --as-needed spelled out

Distribution build defaults often add --as-needed, and the error is sometimes blamed on it. Spelling the option out on the failing link changes nothing:

Terminal
$ g++ main.o -o main -L. -la -Wl,-rpath,. -Wl,--as-needed
/usr/bin/ld: main.o: undefined reference to symbol '_Z8b_answerv'
/usr/bin/ld: ./libb.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

The message is identical. That is because the two options govern different things. --as-needed affects DT_NEEDED tags for libraries mentioned on the command line: a tag is emitted only for a library that, at that point in the link, satisfies a non-weak undefined symbol reference. It decides whether a named library is recorded as needed; it does not decide whether an unnamed library may resolve the program's symbols. The refusal here comes from the other default, covered next.

The legacy form: --copy-dt-needed-entries, and why its position matters

The older linker behavior followed a command-line library's DT_NEEDED entries when resolving symbols, so a program in this shape linked with A alone. --copy-dt-needed-entries restores that behavior: with it in effect, shared libraries on the command line are recursively searched, following their DT_NEEDED tags, to resolve symbols the output binary requires. With the default --no-copy-dt-needed-entries, the search stops with the named library itself and no DT_NEEDED links are traversed. Placed before -la, the option makes the original link succeed:

Terminal
$ g++ main.o -o main_legacy -Wl,--copy-dt-needed-entries -L. -la -Wl,-rpath,.
$ ./main_legacy
43 42

The program prints the same 43 42. But the manual's wording carries a condition: the option applies to the shared libraries that follow it on the command line. Placed after -la, it arrives too late for A, and the identical link fails with the identical message:

Terminal
$ g++ main.o -o main_after -L. -la -Wl,-rpath,. -Wl,--copy-dt-needed-entries
/usr/bin/ld: main.o: undefined reference to symbol '_Z8b_answerv'
/usr/bin/ld: ./libb.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

That position sensitivity is one reason the option is a diagnostic aid rather than the fix. It confirms the diagnosis, the symbol was reachable through A's dependency all along, but a build that relies on it links only as long as every use keeps the option in the right place ahead of the right library. Naming B directly states the program's real dependency in the place every reader and every build tool already looks.

The boundary the error draws is visible from the other side. This program calls only a_answer(); it never names B's function:

C++
#include <iostream>

int a_answer();

int main() {
    std::cout << a_answer() << '\n';
    return 0;
}

Linked with A alone, it succeeds, and the program runs:

Terminal
$ g++ -std=c++20 -c main_a.cpp -o main_a.o
$ g++ main_a.o -o main_a -L. -la -Wl,-rpath,.
$ ./main_a
43

The program prints 43, a value computed inside A by calling B's function. A's own reference to b_answer() is resolved through A's recorded dependency, exactly as it should be: resolving a library's internal references through its own DT_NEEDED entries was never in question. The error appears only at the moment the program's own code reaches past A and calls B directly. That is also the practical test in a large build: if removing the direct call to the lower library makes the error go away, the program was relying on a transitive dependency, and the fix is to promote that dependency to a direct one on the link line.

The exact rule: a direct call needs a direct entry on the link line

For each shared library named on the command line, the default --no-copy-dt-needed-entries behavior stops the symbol search with that library itself: its DT_NEEDED entries are not traversed to resolve symbols required by the program. Those entries still do their own job, the dynamic loader honors them at run time, and the library's own undefined symbols are resolved through them, but they do not add the dependency to the program's link. When the program holds an undefined reference to a symbol that only the dependency defines, the linker reports the undefined reference, then reports the defining file with error adding symbols: DSO missing from command line, and the link fails with exit status 1.

--copy-dt-needed-entries is the documented exception, restoring recursive searching through DT_NEEDED tags for the libraries that follow the option, and --as-needed is a separate control over which named libraries earn a DT_NEEDED tag in the output at all. Neither changes the underlying shape of a correct link: the libraries a program uses directly are the libraries its link line names. A dependency of a dependency is an implementation detail of the middle library, and the link is written so it keeps working if that middle library later stops depending on B, at which point a program that named B itself is unaffected, and a program that borrowed A's dependency breaks.

The fixes, ranked

  • First, add the missing library to the link line: -lb alongside -la in a hand-written command, or the corresponding direct link entry in the build system's target. This is the fix the message is asking for, it names the DSO it could not find on the command line.
  • Second, read the pair of lines together in a large build: the undefined-reference line names the symbol, the DSO line names the library that defines it, and that library is the one to add, not the middle library, which is already there.
  • Third, use --copy-dt-needed-entries, placed before the middle library, only to confirm the diagnosis or to keep a legacy build linking while its link lines are corrected; do not leave it as the standing fix.
  • Fourth, if the direct call was accidental, the program used a lower library's function only because it was visible, remove the call and use the middle library's own interface instead; then A alone is the correct link line again, as the neighbor section shows.

Which fix for which cause

Cause Message Fix
Program calls B's function directly, link line names only A undefined reference to symbol '_Z8b_answerv' and ./libb.so: error adding symbols: DSO missing from command line Add B to the link line next to A (-lb)
Same shape in a build whose defaults add --as-needed Identical pair of lines; the option does not change them Same fix; --as-needed governs DT_NEEDED tags for named libraries, not traversal into unnamed ones
Legacy build that relied on transitive resolution Same pair of lines after a linker or distribution upgrade Add B directly; --copy-dt-needed-entries before the middle library restores the old behavior temporarily
--copy-dt-needed-entries present but the error persists Identical message despite the option Check the option's position: it applies only to libraries named after it, so it must precede the middle library
Program uses only A's functions No error; the link with A alone succeeds and the program runs None needed; A's own references resolve through A's DT_NEEDED entry for B
## Checklist
  • Read the two lines as a pair: the symbol from the undefined-reference line, the library from the DSO line; the library named is the one to add.
  • Add the defining library directly to the program's link line or build target; do not rebuild A or B, and do not change the program's code.
  • Keep the order: object files first, then the libraries they use, each library after the code that references it.
  • Do not blame --as-needed on sight: the failing link prints the identical message with and without it.
  • Treat --copy-dt-needed-entries as position-sensitive: before the middle library it works, after it the same link fails.
  • Apply the boundary test when in doubt: a program that calls only the middle library's functions links with that library alone; the first direct call downward is what needs the new entry.
  • Write links for the program's own calls, not for today's dependency graph: a middle library that drops its dependency later should not break a program that never used that dependency directly.

Sources

  • GNU ld, Options, for --copy-dt-needed-entries making the linker recursively search shared libraries named on the command line, following their DT_NEEDED tags, to resolve symbols required by the output binary, for the default --no-copy-dt-needed-entries behavior stopping that search with the named library itself and traversing no DT_NEEDED links, for the option applying to the shared libraries that follow it, and for --as-needed emitting a DT_NEEDED tag only for a library that at that point in the link satisfies a non-weak undefined symbol reference.

Keep Exploring