go
build_error
ai_generated
true
ld: symbol 'foo' has multiple definitions (from object files a.o and b.o)
ID: go/cgo-multiple-definition-symbol
75%Fix Rate
85%Confidence
1Evidence
2023-11-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Go 1.15 | active | — | — | — |
| Go 1.16 | active | — | — | — |
| Go 1.17 | active | — | — | — |
| Go 1.18 | active | — | — | — |
| Go 1.19 | active | — | — | — |
| Go 1.20 | active | — | — | — |
| Go 1.21 | active | — | — | — |
| Go 1.22 | active | — | — | — |
| Go 1.23 | active | — | — | — |
Root Cause
Duplicate symbol definitions across C object files linked via cgo, often due to including the same .c file in multiple packages or using static libraries with conflicting symbols.
generic中文
通过 cgo 链接的 C 目标文件之间存在重复符号定义,通常是由于在多个包中包含相同的 .c 文件或使用具有冲突符号的静态库。
Official Documentation
https://pkg.go.dev/cmd/cgoWorkarounds
-
85% success Ensure each .c file is compiled only once by consolidating cgo directives into a single package or using a single .go file with all C code
Ensure each .c file is compiled only once by consolidating cgo directives into a single package or using a single .go file with all C code
-
70% success Use linker flag -Wl,--allow-multiple-definition to force link but verify behavior
Use linker flag -Wl,--allow-multiple-definition to force link but verify behavior
-
80% success Refactor to avoid static libraries with overlapping symbols; use dynamic linking or rename symbols via objcopy
Refactor to avoid static libraries with overlapping symbols; use dynamic linking or rename symbols via objcopy
中文步骤
Ensure each .c file is compiled only once by consolidating cgo directives into a single package or using a single .go file with all C code
Use linker flag -Wl,--allow-multiple-definition to force link but verify behavior
Refactor to avoid static libraries with overlapping symbols; use dynamic linking or rename symbols via objcopy
Dead Ends
Common approaches that don't work:
-
60% fail
This flag suppresses the error but can cause undefined behavior or crashes due to symbol aliasing.
-
50% fail
If the duplicate comes from a static library you don't control, renaming isn't possible.
-
90% fail
Multiple definitions occur at link time from different .o files, not from header inclusion; pragma doesn't help.