简体   繁体   English

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

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

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). 例如,假设我在一台计算机上编写了一个Fortran程序,并希望在另一台计算机上运行该程序,而该计算机可能没有必需的库和/或具有不同的编译器(甚至更好,根本没有Fortran编译器)。 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. 我在Fedora 26中使用gfortran(7.2.1),有时在我的代码中使用LAPACK例程。

Using -static option with the program 在程序中使用-static选项

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 gfortran -static-libgfortran a.f90没有错误

In Fedora, gcc does not ship by default with static libraries. 在Fedora中,默认情况下,静态库不附带gcc。

You need to install the package glibc-static for the -static option to work, as hinted in this related question . 您需要安装glibc-static软件包才能使-static选项正常工作,如相关问题所述

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. 请注意, -static-libgfortran将仅对libgfortran库进行静态链接,并且您还必须具有依赖项的静态版本。

The best option is to use Alpine Linux that uses musl libc . 最好的选择是使用使用musl libc的 Alpine Linux。 I highly recommend using the docker image (only 5 Mb). 我强烈建议使用docker 映像 (仅5 Mb)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM