是否可以在 Linux 中编译独立的 Fortran 可执行文件?

Is it possible to compile a standalone Fortran executable in Linux?

例如,假设我在一台计算机上编写了一个 Fortran 程序,并想在另一台计算机上 运行 它可能没有所需的库 and/or 有不同的编译器(或者更好,根本没有 Fortran 编译器)。是否可以创建一个包含所有依赖项的可执行文件?

我在 Fedora 26 中使用 gfortran (7.2.1),有时在我的代码中使用 LAPACK 例程。

在程序中使用 -static 选项

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

我得到输出

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

gfortran -static-libgfortran a.f90

没有错误

在 Fedora 中,gcc 默认不附带静态库。

您需要安装软件包 glibc-static 才能使 -static 选项正常工作,如 .

中所暗示

请注意 -static-libgfortran 只会对 libgfortran 库进行静态链接,并且您还必须拥有依赖项的静态版本。

最好的选择是使用 Alpine Linux,它使用 musl libc. I highly recommend using the docker image(仅 5 Mb)。