MacOS下使用Azure Speech Golang SDK遇x86_64架构未定义符号错误
问题描述
按照Azure Speech SDK的Go语言官方安装指南配置环境后,编译时出现Undefined symbols for architecture x86_64错误,调整架构参数后仍无法解决。具体错误信息如下:
/usr/local/Cellar/go/1.20.5/libexec/pkg/tool/darwin_amd64/link: running cc failed: exit status 1 ld: warning: -no_pie is deprecated when targeting new OS versions ld: warning: ignoring file <speech-sdk-path>/lib/x86/libMicrosoft.CognitiveServices.Speech.core.so, building for macOS-x86_64 but attempting to link with file built for unknown-unsupported file format ( 0x7F 0x45 0x4C 0x46 0x01 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 ) Undefined symbols for architecture x86_64: "_add_source_lang_config_to_auto_detect_source_lang_config", referenced from: __cgo_c0b4197be0b1_Cfunc_add_source_lang_config_to_auto_detect_source_lang_config in 000011.o
解决方法
核心问题是用错了平台的SDK库文件:错误信息里的libMicrosoft.CognitiveServices.Speech.core.so是Linux平台的ELF格式库,而当前编译环境是macOS x86_64,macOS使用的是Mach-O格式的.dylib库,链接器无法识别.so文件,导致符号未定义。
按以下步骤修复:
- 重新下载对应平台的SDK:获取macOS x86_64架构的Azure Speech SDK包,不要下载Linux版本。
- 替换库路径:将项目中引用的SDK路径替换为macOS SDK解压后的路径,确保链接的是
lib/x64/libMicrosoft.CognitiveServices.Speech.core.dylib。 - 配置正确的环境变量:设置
CGO_CFLAGS和CGO_LDFLAGS指向macOS SDK的头文件和库路径,示例:export CGO_CFLAGS="-I/你的macOS SDK路径/include" export CGO_LDFLAGS="-L/你的macOS SDK路径/lib/x64 -lMicrosoft.CognitiveServices.Speech.core" - 清理编译缓存后重新编译:执行
go clean,再运行go build或go run。
内容的提问来源于stack exchange,提问作者Onur Yıldırım
相关产品推荐
相关产品推荐

