在 macOS 上将 Fortran 包装在 Go 中:ld: 找不到 -lgfortran 库。

huangapple go评论87阅读模式
英文:

wrapping fortran in go on macOS: ld: library not found for -lgfortran

问题

我正在为现代化科学模型将一个Fortran程序封装在Go包装器中,作为前提条件,这个程序在Windows上可以运行。我现在有一台Mac电脑,想知道如何在M2芯片上运行它。以下是我在Mac上所做的工作:

  • 通过brew安装了gccgfortran
  • 通过go build ./cmd/fort进行构建
  • 根据cgo文档添加了// #cgo LDFLAGS: -v

这是一个包含我目前工作的存储库:

当我在ldflags配置中使用-v时,出现了错误:

# github.com/marty-farce/fort/cmd/fort
/opt/homebrew/Cellar/go/1.20.6/libexec/pkg/tool/darwin_arm64/link: running cc failed: exit status 1
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin22.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
 "/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -dynamic -arch arm64 -platform_version macos 13.0.0 13.3 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -o $WORK/b001/exe/a.out -L/usr/local/lib -headerpad 1144 /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/go.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000000.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000001.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000002.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000003.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000004.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000005.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000006.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000007.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000008.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000009.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000010.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000011.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000012.o
-lgfortran -framework CoreFoundation -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/14.0.3/lib/darwin/libclang_rt.osx.a
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)

主要问题是链接器无法找到gfortran库,尽管它正在寻找-lgfortran,我怀疑我没有正确地包含该库。我尝试修改cgo LDFLAGS变量,将gfortran二进制文件的位置包含在LDFLAGS变量行中,如下所示:

// #cgo LDFLAGS: -L/opt/homebrew/bin -v

但这并没有改变链接器寻找gfortran库的位置,有没有链接器方面的人可以帮助我?

英文:

I am wrapping a fortran program in a go wrapper for modernizing scientific models, as a prerequisite this works on Windows. I got a mac and am curious on how to make it run on the M2 chip. here is what I have done so far on my mac:

  • installed gcc and gfortran through brew
  • built it through go build ./cmd/fort
  • added the // #cgo LDFLAGS: -v per the cgo documentation

here is a repo with the work I have so far:

I get the error when I use -v in the ldflags configuration

# github.com/marty-farce/fort/cmd/fort
/opt/homebrew/Cellar/go/1.20.6/libexec/pkg/tool/darwin_arm64/link: running cc failed: exit status 1
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin22.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
 "/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -dynamic -arch arm64 -platform_version macos 13.0.0 13.3 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -o $WORK/b001/exe/a.out -L/usr/local/lib -headerpad 1144 /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/go.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000000.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000001.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000002.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000003.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000004.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000005.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000006.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000007.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000008.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000009.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000010.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000011.o /var/folders/w1/nf_07l0x6xq6t9yx9wdz9vlw0000gn/T/go-link-2078530619/000012.o
-lgfortran -framework CoreFoundation -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/14.0.3/lib/darwin/libclang_rt.osx.a
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)

the main thing is the linker cannot find the library for gfortran, despite looking for "-lgfortran", I suspect I am not including the library properly. I tried messing with the cgo LDFLAGS variable to include the location of the gfortran binary making the LDFLAGS variable line look like this

// #cgo LDFLAGS: -L/opt/homebrew/bin -v

but that didn't change where the linker is looking for the gfortran library, are there any linker people who can help me out?

答案1

得分: 0

你需要做的是找到你的"libgfortran"库的位置,macOS有一个非传统的文件系统(我是新手,但我来自Linux背景),我是这样做的:

tree . > ~/Desktop/file

然后在生成的文件中搜索'libgfortran'。我得到了路径"-L/opt/homebrew/lib/gcc/13",将其放入应用程序中,现在它可以构建,因为找到了库。我在仓库中添加了一个提交来展示我的意思。

英文:

what you need to do is find the location of your "libgfortran" library, macos has an unconventional file system (I'm new to mac but I come from a linux background), the way I did this is

tree . > ~/Desktop/file

and made a search for 'libgfortran' in that treed file. I got the path "-L/opt/homebrew/lib/gcc/13", put that in the application, and now it builds because it found the library. I added a commit to the repo to show what I mean

huangapple
  • 本文由 发表于 2023年7月27日 10:43:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76776180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定