Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to statically link in gfortran libraries on OSX

Tags:

macos

gfortran

I've a fortran program I'd like to distribute, so I'd like to statically link in the gfortran libraries.

If I compile the program with the following flags:

gfortran -o myprog -static-libgfortran -static-libgcc  myprog.f

otool tells me it's statically linked in most of the gofrtran libraries, but not libquadmath:

otool -L myprog

/usr/local/gfortran/lib/libquadmath.0.dylib (compatibility version 1.0.0, current v
        /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)

There is a static libquadmath library /usr/local/gfortran/lib/libquadmath.a, but every link line I tried always either ended up with a full static link (which isn't supported on OSX) or a dynamic link to libquadmath.

I've managed to create what I want by removing libquadmath.0.dylib and libquadmath.dylib from /usr/local/gfortran/lib/, and the linker then pulls in the static library.

However, this seems somewhat clunky to say the least.

Can anyone suggest a more elegant way of doing this?

Thanks!

like image 719
linucks Avatar asked Jul 11 '13 10:07

linucks


1 Answers

I know this is very old tracker, but maybe somebody will be still interested in the solution that works.

Let's say we have code:

! fort_sample.f90
program main
  write (*,*) 'Hello'
  stop
end

First, compile the stuff:

gfortran -c -o fort_sample.o fort_sample.f90

Then, link stuff

ld -o ./fort_sample -no_compact_unwind \
-arch x86_64 -macosx_version_min 10.12.0 \
-lSystem \
/usr/local/gfortran/lib/libgfortran.a \
/usr/local/gfortran/lib/libquadmath.a \
/usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0/libgcc.a \
fort_sample.o

You can execute it

./fort_sample
 Hello

You can notice that quadmath is no longer there

> otool -L fort_sample
fort_sample:
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.51.1)

I guess this is what you were looking for in a first place. No removing dylibs, no symbolic links, etc.

like image 73
Oo.oO Avatar answered Oct 13 '22 21:10

Oo.oO