go
build_error
ai_generated
true
ld:符号 'foo' 有多个定义(来自目标文件 a.o 和 b.o)
ld: symbol 'foo' has multiple definitions (from object files a.o and b.o)
ID: go/cgo-multiple-definition-symbol
75%修复率
85%置信度
1证据数
2023-11-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 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 | — | — | — |
根因分析
通过 cgo 链接的 C 目标文件之间存在重复符号定义,通常是由于在多个包中包含相同的 .c 文件或使用具有冲突符号的静态库。
English
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.
官方文档
https://pkg.go.dev/cmd/cgo解决方案
-
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
无效尝试
常见但无效的做法:
-
60% 失败
This flag suppresses the error but can cause undefined behavior or crashes due to symbol aliasing.
-
50% 失败
If the duplicate comes from a static library you don't control, renaming isn't possible.
-
90% 失败
Multiple definitions occur at link time from different .o files, not from header inclusion; pragma doesn't help.