1

For example, consider that I wrote a Fortran program in one computer and want to run it on another computer which may not have required libraries and/or has a different compiler (or even better, no Fortran compiler at all). Is it possible to create an executable with all its dependencies?

I am using gfortran (7.2.1) in Fedora 26 and sometimes use LAPACK routines in my code.

Using -static option with the program

program main
 write (*,*) 'Hello'
end

I get the output

gfortran -static a.f90 
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status

There is no error with gfortran -static-libgfortran a.f90

francescalus
  • 30,576
  • 16
  • 61
  • 96
Sumit
  • 211
  • 5
  • 10
  • 1
    Read about static compilation and static linking. Not everything can be statically linked, there are issues with GLIBC. Just try compiling/linking with the `-static` flag first. – Vladimir F Героям слава Feb 02 '18 at 10:21
  • Thanks @VladimirF. When I use `-static` flag (to print "hello world") it shows `/usr/bin/ld: cannot find -lm`, `/usr/bin/ld: cannot find -lm`, `/usr/bin/ld: cannot find -lc`. What do I have to install for them? It does not show any error if I use `gfortran -static-libgfortran a.f90`. – Sumit Feb 02 '18 at 10:55
  • 1
    Static linking of libgfortran is done via the extra switch. To link statically LAPACK, you must have a static LAPACK library available and also provide `-static` anyway. Please copy-paste your error in the main question as formatting of comments is terrible. – Pierre de Buyl Feb 02 '18 at 11:12
  • @PierredeBuyl, I edit my question. – Sumit Feb 02 '18 at 11:18
  • 2
    For what it's worth, I can compile (on Debian) with `gfortran-7 -static a.f90`. Apparently in Fedora, you need the package `glibc-static` (see this other [question](https://stackoverflow.com/questions/41002773/fedora-dynamic-replacement-for-libm-astatic-lib). If that works, I'll post it as an answer. – Pierre de Buyl Feb 02 '18 at 11:32
  • It works @PierredeBuyl. `glibc-static` does the trick. – Sumit Feb 02 '18 at 12:40
  • @Sumit when you visit Stack Overfow next, could you mark the answer as accepted? – Pierre de Buyl Feb 02 '18 at 15:18

2 Answers2

4

In Fedora, gcc does not ship by default with static libraries.

You need to install the package glibc-static for the -static option to work, as hinted in this related question.

Note that -static-libgfortran will only do static linking of the libgfortran library and that you must have static versions of your dependencies as well.

Pierre de Buyl
  • 7,074
  • 2
  • 16
  • 22
0

The best option is to use Alpine Linux that uses musl libc. I highly recommend using the docker image (only 5 Mb).

stali
  • 321
  • 2
  • 9