在 macOS 中執行 Fortran

Galois發表於2020-05-22

在 Mac 中,fortran 被整合在 gcc 中,而 MacOS 本身不提供 gcc 而是 clang,因而可以藉助 brew 安裝:

$ brew install gcc

以下是 helloworld 程式,儲存為 xx.f90

program hello
print *, "Hello World"
end program hello

在 terminal 中執行,與執行 C、C++ 程式非常相似。

$ gfortran xx.f90 -o xx
$ ./xx

儲存為檔案命名字尾應該是 f90,不是 f,可以看看 stackoverflow上的解釋。
f90 給予了更加自由的程式編寫格式,fortran 原本的設計目的是為了公式推導科學計算。

如果不安裝 fortran 可能會遇到一些問題,例如:

$ pip install pymc
# Error information
    don't know how to compile Fortran code on platform 'posix'
    warning: build_ext: f77_compiler=None is not available.

    building 'pymc.flib' extension
    error: extension 'pymc.flib' has Fortran sources but no Fortran compiler found

例如:

$ easy_install pymc
# Error information
Could not locate executable gfortran
Could not locate executable f95
Could not locate executable f90
Could not locate executable f77
Could not locate executable xlf90
Could not locate executable xlf
Could not locate executable ifort
Could not locate executable ifc
Could not locate executable g77
Could not locate executable g95
Could not locate executable pgfortran
don't know how to compile Fortran code on platform 'posix'
warning: build_ext: f77_compiler=None is not available.

error: Setup script exited with error: extension 'pymc.flib' has Fortran sources but no Fortran compiler found
本作品採用《CC 協議》,轉載必須註明作者和本文連結
不要試圖用百米衝刺的方法完成馬拉松比賽。

相關文章