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

时间:2018-02-02 10:16:03

标签: linux fortran executable

例如,考虑我在一台计算机上编写了Fortran程序,并希望在另一台可能没有所需库和/或具有不同编译器的计算机上运行它(或者甚至更好,根本没有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

没有错误

2 个答案:

答案 0 :(得分:3)

在Fedora中,默认情况下gcc不会附带静态库。

您需要为glibc-static选项安装程序包-static才能正常工作,如related question中所示。

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

答案 1 :(得分:0)

最好的选择是使用使用here的Alpine Linux。我强烈建议使用docker musl libc(仅5 Mb)。