M1 Macbook Air(Ventura13.1-arm64)gfortran12.2.0编译报错求助
在M1 Mac (Ventura 13.1 arm64) 上解决gfortran 12.2.0
ld: library not found for -lSystem 错误 问题场景
在搭载MacOS Ventura 13.1-arm64的M1 MacBook Air上,使用gfortran 12.2.0编译以下Fortran示例程序:
program main print *, "Hello world." end program main
执行编译命令:
gfortran hello.f95 -o hello
出现报错:
ld: library not found for -lSystem collect2: error: ld returned 1 exit status
添加库路径参数后又出现架构不匹配警告:
gfortran hello.f95 -o hello -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
ld: warning: ignoring file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd, missing required architecture arm64 in file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd
注:使用系统自带gcc编译等效C程序无异常,已尝试升级系统、通过Homebrew更新gcc、改用fxcoudert的gfortran安装包,均未解决。当前环境:Xcode14.2(14C18),gfortran路径/usr/local/bin/gfortran,gcc路径/usr/bin/gcc,ld路径/usr/bin/ld。
解决方法
方法1:修复Command Line Tools SDK配置
- 卸载现有Command Line Tools:
sudo rm -rf /Library/Developer/CommandLineTools
- 重新安装Command Line Tools:
xcode-select --install
- 验证安装路径:
xcode-select -p
输出应为/Library/Developer/CommandLineTools。
4. 设置永久生效的SDKROOT环境变量:
- 若使用zsh,编辑
~/.zshrc,添加:export SDKROOT=$(xcrun --show-sdk-path) - 若使用bash,编辑
~/.bashrc,添加上述内容。 - 执行
source ~/.zshrc或source ~/.bashrc使配置立即生效。
方法2:编译时强制指定架构与SDK路径
直接在编译命令中指定arm64架构和系统SDK路径:
gfortran -arch arm64 -isysroot $(xcrun --show-sdk-path) hello.f95 -o hello
方法3:确保Homebrew与gfortran为原生arm64版本
- 检查gfortran架构:
file /usr/local/bin/gfortran
输出需包含arm64,若显示x86_64,说明是通过Rosetta安装的x86版本,需重新安装Homebrew:
2. 卸载现有Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
- 关闭所有终端,重新打开非Rosetta模式的终端(打开终端时不要勾选“使用Rosetta打开”),重新安装Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- 重新安装gcc(包含gfortran):
brew install gcc
问题原因
M1 Mac的arm64架构要求链接原生arm64版本的系统库,若Command Line Tools的SDK损坏、架构不匹配,或gfortran为x86_64版本(依赖Rosetta),会导致链接器无法找到对应架构的libSystem库,从而出现上述错误或警告。
内容的提问来源于stack exchange,提问作者user20907273
相关产品推荐
相关产品推荐

