Distributing gfortran-compiled binary to Mac OS X users who don't have Xcode?

Viewed 539

I'm teaching a class in which I would like students to run a precompiled command-line program on their own laptops. Some students have Windows; others have Mac. I was able to create a self-contained binary that the Windows users can just drop into their working directory and run.

To my surprise, I'm finding it far more difficult to do the same thing for Mac users. In particular, it appears to be impossible to compile a standalone binary program with all required libraries statically linked. The particular libraries that need to be present are libgfortran and libquadmath, since the gfortran compiler is used to compile the legacy F77 code (and it's too big a program to convert to another language).

So the only potentially workable solution I've found so far is for students to first install Xcode, then Xcode's command line tools, and finally gfortran on their own computer before my executable will find the gfortran library and run. But this is way too much work (and disk space) for what I want them to do.

My question: is there a way I'm overlooking to compile my program in completely self-contained form? Obviously it's hard for me to experiment, because it involves other people's computers.

EDIT: I found the following post which describes essentially the same problem: https://groups.google.com/forum/#!topic/comp.lang.fortran/TPEQQEwa0HI Unfortunately, the solution given doesn't work for me, even after I delete some options and make appropriate substitutions.

2 Answers

The solution you linked to includes the -static-libgfortran, but not -static-libgcc, so it may be worth adding the second of these.

For a barebones Hello World code, I get:

[~] oldrabbit $ otool -L a.out 
a.out:
    /usr/local/opt/gcc/lib/gcc/8/libgfortran.5.dylib (compatibility version 6.0.0, current version 6.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
    /usr/local/lib/gcc/8/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
    /usr/local/opt/gcc/lib/gcc/8/libquadmath.0.dylib (compatibility version 1.0.0, current version 1.0.0)
[~] oldrabbit $ gfortran -static-libgfortran -static-libgcc hello.f90 
[~] oldrabbit $ otool -L a.out 
a.out:
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
    /usr/local/opt/gcc/lib/gcc/8/libquadmath.0.dylib (compatibility version 1.0.0, current version 1.0.0)

so looks like the libquadmath library might still be a problem. If you can find the path to the .a version of that, you could try including it like in the example you linked to.

a solution would be to ship the executable and the libquadmath by using install_name_tool:

cp -f /usr/local/Cellar/gcc/10.2.0_4/lib/gcc/10/libquadmath.0.dylib .
install_name_tool -change $i @executable_path/libquadmath.0.dylib a.out 

Of course the second command must be done every time you recompile your binary

Related