Ubuntu上部署Fortran应用的实用流程
一 环境准备与编译器安装
sudo apt update && sudo apt install gfortransudo apt install build-essential cmakegfortran --version(输出版本号即正常)二 构建与运行最简示例
program hello
print *, "Hello, Fortran on Ubuntu!"
end program hello
gfortran -O2 -Wall -o hello hello.f90./hello三 依赖库与工程化部署
sudo apt install libnetcdf-dev libnetcdff-devgfortran test.f90 \nf-config --fflags --flibs` -o test`./testcmake_minimum_required(VERSION 3.16)
project(hello Fortran)
find_package(NetCDF REQUIRED Fortran)
add_executable(hello hello.f90)
target_link_libraries(hello ${NETCDF_LIBRARIES})
target_include_directories(hello PRIVATE ${NETCDF_INCLUDE_DIRS})
mkdir -p build && cd build && cmake -DCMAKE_INSTALL_PREFIX=/opt/helloapp .. && make -j$(nproc) && sudo make install四 多文件工程与发布打包
gfortran -c mod.f90 -o mod.o && gfortran -c main.f90 -o main.o && gfortran -o app main.o mod.oapp/(可执行文件)、lib/(第三方动态库)、share/(文档/示例)、bin/(启动脚本)、etc/(配置)#!/usr/bin/env bash
HERE=$(cd "$(dirname "$0")" && pwd)
export LD_LIBRARY_PATH="$HERE/../lib:$LD_LIBRARY_PATH"
exec "$HERE/../app/hello" "$@"
sudo make install(配合 CMake 的 install 目标或自定义 Makefile)uninstall 目标,或记录安装文件清单后批量删除五 常见问题与处理
-I<头文件路径> -L<库路径> -l<库名>;链接 NetCDF-Fortran 时优先用 nf-config --fflags --flibs 获取参数export LD_LIBRARY_PATH=/opt/helloapp/lib:$LD_LIBRARY_PATH,或在 /etc/ld.so.conf.d/ 下添加库路径后执行 sudo ldconfig