The "DSO missing from command line" error is a common issue encountered during the linking stage of compilation, particularly when working with dynamic shared objects (DSOs) or shared libraries. This tutorial aims to provide a clear understanding of the cause of this error and offer practical solutions to resolve it.
Understanding the Error
The "DSO missing from command line" error typically occurs when the linker is unable to find a required symbol in the specified libraries, despite the symbol being available in one of the dependencies of a directly specified dynamic library. This can happen due to changes in the way the linker searches for symbols in later versions.
Causes of the Error
- Library Order: The order in which libraries are specified on the command line matters. Symbols are first requested, and then linked from a library that has them. If a library is specified before the object files that use it, the linker may not be able to find the required symbols.
- Missing Libraries: Failing to specify all necessary libraries on the command line can lead to this error. The linker needs to know about all dependencies to resolve symbols correctly.
- Circular Dependencies: In cases where there are circular dependencies between libraries (e.g., libA depends on libB, and libB depends on libA), specifying the same library multiple times on the command line may be necessary.
Solutions
-
Specify Libraries Correctly:
- Ensure that all libraries needed to satisfy required symbols are directly specified on the linker command line.
- The order often matters: object files or static libraries should come before dynamic libraries.
- Example:
gcc x.o y.o z.o -la -lb -lc
-
Handle Circular Dependencies:
- If libB needs a symbol from libc, and libc needs a symbol from libB, you might need to specify libB twice:
gcc x.o y.o z.o -la -lb -lc -lb
- If libB needs a symbol from libc, and libc needs a symbol from libB, you might need to specify libB twice:
-
Use the
-Wl,--copy-dt-needed-entries
Option:- As a workaround for maintainers or users of software, adding this option before linked libraries can help by reverting to a more permissive view of available symbols:
g++ main.cc -Wl,--copy-dt-needed-entries -ltensorflow
- This can also be achieved by exporting
LDFLAGS
with the-Wl,--copy-dt-needed-entries
option before running configure or make.
- As a workaround for maintainers or users of software, adding this option before linked libraries can help by reverting to a more permissive view of available symbols:
-
Check for Missing Libraries:
- Ensure that all required libraries are included in the compilation command. If a library is missing, add it to the linker flags (e.g.,
-lz
for libz).
- Ensure that all required libraries are included in the compilation command. If a library is missing, add it to the linker flags (e.g.,
Example
Consider a scenario where you’re compiling a program myprogram
that depends on libpthread
. The correct compilation command would ensure that object files come before libraries and all necessary libraries are included:
gcc myprogram.o -o myprogram -lpthread -lrt -lm
In this example, myprogram.o
is compiled into an executable named myprogram
, linking against libpthread
, librt
, and libm
.
Conclusion
Resolving the "DSO missing from command line" error involves understanding the linker’s behavior regarding library dependencies and ensuring that all necessary libraries are correctly specified on the command line. By following the guidelines outlined in this tutorial, developers can efficiently diagnose and fix this common issue, streamlining their compilation process.