Search code examples
macosgolinker-errorsgfortran

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


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?


Solution

  • 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