go build_error ai_generated true

error: undefined reference to `some_function'

ID: go/cgo-ldflags-ordering

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2024-10-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active
1.19 active

Root Cause

Library order in `LDFLAGS` matters; if a library depends on another, it must come after.

generic

中文

`LDFLAGS`中的库顺序很重要;如果一个库依赖于另一个库,它必须放在后面。

Workarounds

  1. 90% success Order libraries so that dependencies come after dependents
    // #cgo LDFLAGS: -lbar -lfoo  // if foo depends on bar
  2. 80% success Use `-Wl,--as-needed` to allow reordering
    // #cgo LDFLAGS: -Wl,--as-needed -lfoo -lbar

Dead Ends

Common approaches that don't work:

  1. Adding `-lfoo` multiple times 60% fail

    Does not fix dependency order.

  2. Using `-Wl,--start-group ... --end-group` incorrectly 40% fail

    May cause circular dependency issues.