1. 安裝Fortran編譯器
在Debian系統上,gfortran(GNU Fortran編譯器)是最常用的開源Fortran編譯器,可通過系統包管理器直接安裝。打開終端,依次執行以下命令更新包列表并安裝gfortran:
sudo apt update
sudo apt install gfortran
安裝完成后,通過gfortran --version
命令驗證安裝是否成功(終端會顯示gfortran的版本信息,如“GNU Fortran (Debian 12.2.0-14) 12.2.0”)。
2. 配置開發工具(可選但推薦)
為提升開發效率,建議安裝代碼編輯器或集成開發環境(IDE),并配置Fortran語言支持:
sudo dpkg -i code_*.deb
)。打開VS Code,進入擴展視圖(快捷鍵Ctrl+Shift+X
),搜索并安裝以下擴展:
sudo apt install codeblocks
)或Eclipse + Photran插件(需額外配置)。3. 編寫并運行測試程序
用文本編輯器(如VS Code、Nano或Vim)創建一個簡單的Fortran程序(如hello.f90
),內容如下:
program hello
print *, "Hello, World!" ! 輸出Hello World
end program hello
保存文件后,在終端中導航至文件所在目錄,執行以下命令編譯并運行:
gfortran hello.f90 -o hello # 編譯生成可執行文件hello
./hello # 運行程序
若終端輸出“Hello, World!”,則說明Fortran開發環境配置成功。
4. 安裝常用依賴庫(可選)
若項目需要使用科學計算或并行計算庫,可通過apt安裝常用依賴:
sudo apt install libblas-dev liblapack-dev
);sudo apt install libopenmpi-dev
);sudo apt install libopenmp-dev
)。5. 項目構建與管理
gfortran -o output source.f90
編譯;多文件程序需指定所有源文件(如gfortran -o output main.f90 module1.f90 module2.f90
)。FC = gfortran
FFLAGS = -O2
SRCS = main.f90 utils.f90
OBJS = $(SRCS:.f90=.o)
TARGET = my_program
all: $(TARGET)
$(TARGET): $(OBJS)
$(FC) $(FFLAGS) -o $@ $^
%.o: %.f90
$(FC) $(FFLAGS) -c $<
clean:
rm -f $(OBJS) $(TARGET)
執行make
編譯項目,make clean
清理生成的文件。wget https://github.com/fortran-lang/fpm/releases/download/v0.9.0/fpm-0.9.0-linux-x86_64 -O /usr/local/bin/fpm
chmod +x /usr/local/bin/fpm
fpm new my_project # 創建新項目
cd my_project
fpm build # 構建項目
fpm run # 運行項目
```。
6. 可選:配置環境變量(針對特定場景)
若安裝了多個Fortran編譯器(如gfortran與Intel Fortran)或自定義庫,需配置環境變量:
export PATH=/usr/local/gfortran/bin:$PATH
);export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
)。~/.bashrc
文件(用戶級配置)或/etc/profile
(系統級配置),添加上述命令后,執行source ~/.bashrc
使配置生效。