go build_error ai_generated true

error: cgo: dynamic library not found: libfoo.so: cannot open shared object file: No such file or directory

ID: go/cgo-dynamic-library-not-found

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2025-02-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active
1.21 active

Root Cause

Dynamic library (`.so`) not in standard library paths or `LD_LIBRARY_PATH`.

generic

中文

动态库(`.so`)不在标准库路径或`LD_LIBRARY_PATH`中。

Workarounds

  1. 90% success Set `LD_LIBRARY_PATH` before running
    export LD_LIBRARY_PATH=/path/to/lib:$LD_LIBRARY_PATH
    ./program
  2. 85% success Use `-rpath` in LDFLAGS with absolute path
    // #cgo LDFLAGS: -rpath,/path/to/lib -L/path/to/lib -lfoo

Dead Ends

Common approaches that don't work:

  1. Copying .so to current directory 70% fail

    Current directory not searched by default.

  2. Using `-rpath` without absolute path 50% fail

    Relative rpath may break if binary moved.