这是因为 whisper.cpp 的 Go 绑定需要 CGO 编译,并且需要正确链接到 whisper.cpp 的 C 库。错误信息显示找不到 C 的类型定义。
解决方案:
- 确保已安装 whisper.cpp 到系统:
# 克隆并编译 whisper.cpp
git clone https://github.com/ggerganov/whisper.cpp.git
cd whisper.cpp
cmake -B build
cmake --build build -j
sudo cmake --install build # 安装到 /usr/local
- 设置 CGO 环境变量:
export CGO_ENABLED=1
export CGO_LDFLAGS="-L/usr/local/lib"
export CGO_CFLAGS="-I/usr/local/include"
export LD_LIBRARY_PATH="/usr/local/lib:$LD_LIBRARY_PATH"
- 清理 Go 缓存并重新编译:
cd backend
go clean -cache
go mod tidy
go build
- 验证库文件存在:
ls -l /usr/local/lib/libwhisper.*
ls -l /usr/local/include/whisper.h
如果你使用的是 macOS,可能还需要:
# macOS 使用 Homebrew 安装可能更简单
brew install whisper-cpp
# 或者手动指定路径
export CGO_LDFLAGS="-L/opt/homebrew/lib"
export CGO_CFLAGS="-I/opt/homebrew/include"
注意:whisper.cpp 的 Go 绑定要求本地必须有编译好的 whisper.cpp C 库,这就是为什么 Dockerfile.base 中需要编译安装 whisper.cpp 的原因。
案例1
#!/bin/bash
# 设置 whisper.cpp 库路径
export DYLD_LIBRARY_PATH="/usr/local/lib:$DYLD_LIBRARY_PATH"
export CGO_ENABLED=1
export CGO_LDFLAGS="-L/usr/local/lib"
export CGO_CFLAGS="-I/usr/local/include"
# 运行应用
go run main.go