Files
2025-12-14 15:25:31 +08:00

89 lines
2.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 编译说明 - 兼容旧版本系统
## 问题说明
如果在 Debian 11 或其他旧版本系统上运行时出现 GLIBC 版本错误:
```
./mengyamonitor-backend: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.34' not found
```
这是因为编译时使用了较新版本的 GLIBC而目标系统的 GLIBC 版本较旧。
## 解决方案
### 方案 1在目标系统上编译推荐
在 Debian 11 服务器上直接编译:
```bash
cd mengyamonitor-backend
# 禁用 CGO静态链接
export CGO_ENABLED=0
go build -ldflags="-s -w" -o mengyamonitor-backend .
# 或者使用提供的脚本
chmod +x build.sh
./build.sh
```
### 方案 2使用静态链接编译
在任何系统上编译,但使用静态链接:
```bash
cd mengyamonitor-backend
# 禁用 CGO
export CGO_ENABLED=0
export GOOS=linux
export GOARCH=amd64
# 编译(静态链接,不依赖系统库)
go build -ldflags="-s -w" -o mengyamonitor-backend .
```
### 方案 3使用 Docker 编译
使用 Docker 在 Debian 11 环境中编译:
```bash
# 使用 Debian 11 镜像编译
docker run --rm -v $(pwd):/app -w /app golang:1.21-bullseye sh -c "
export CGO_ENABLED=0
go build -ldflags='-s -w' -o mengyamonitor-backend .
"
# 或者使用多阶段构建
docker build -t mengyamonitor-backend -f Dockerfile.build .
```
## 验证编译结果
编译完成后,检查二进制文件:
```bash
# 检查文件类型
file mengyamonitor-backend
# 检查依赖(如果是静态链接,应该显示 "not a dynamic executable"
ldd mengyamonitor-backend
# 检查 GLIBC 依赖(应该没有或很少)
objdump -T mengyamonitor-backend | grep GLIBC
```
## 编译参数说明
- `CGO_ENABLED=0`: 禁用 CGO使用纯 Go 实现,不依赖系统 C 库
- `-ldflags="-s -w"`: 减小二进制文件大小
- `-s`: 省略符号表和调试信息
- `-w`: 省略 DWARF 符号表
## 注意事项
1. 禁用 CGO 后,某些需要 C 库的功能可能不可用,但本项目不依赖 CGO
2. 静态链接的二进制文件会稍大一些,但兼容性更好
3. 如果必须使用 CGO需要在目标系统上编译或使用相同 GLIBC 版本的系统编译