# ld：符号 'foo' 有多个定义（来自目标文件 a.o 和 b.o）

- **ID:** `go/cgo-multiple-definition-symbol`
- **领域:** go
- **类别:** build_error
- **验证级别:** ai_generated
- **修复率:** 75%

## 根因

通过 cgo 链接的 C 目标文件之间存在重复符号定义，通常是由于在多个包中包含相同的 .c 文件或使用具有冲突符号的静态库。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 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 | — | — |

## 解决方案

1. ```
   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
   ```
2. ```
   Use linker flag -Wl,--allow-multiple-definition to force link but verify behavior
   ```
3. ```
   Refactor to avoid static libraries with overlapping symbols; use dynamic linking or rename symbols via objcopy
   ```

## 无效尝试

- **** — This flag suppresses the error but can cause undefined behavior or crashes due to symbol aliasing. (60% 失败率)
- **** — If the duplicate comes from a static library you don't control, renaming isn't possible. (50% 失败率)
- **** — Multiple definitions occur at link time from different .o files, not from header inclusion; pragma doesn't help. (90% 失败率)
