Ubuntu上Fortran跨平台开发实践指南
一 基础环境与工具链
sudo apt update && sudo apt install gfortran,验证版本:gfortran --version。wget https://github.com/fortran-lang/fpm/releases/download/v0.9.0/fpm-0.9.0-linux-x86_64 -O fpm && chmod +x fpm && sudo mv fpm /usr/local/bin/fpm2,随后 fpm new mytest && cd mytest && fpm run。二 构建系统与可复现性
cmake_minimum_required(VERSION 3.16)
project(hello LANGUAGES Fortran)
set(CMAKE_Fortran_STANDARD 2008)
add_executable(hello main.f90)
enable_testing()
add_test(NAME run_hello COMMAND hello)
构建:mkdir -p build && cd build && cmake -DCMAKE_BUILD_TYPE=Release .. && cmake --build .三 跨平台编码要点
implicit none,避免使用已弃用/非标准扩展。gfortran simple.f90 $(pkg-config --cflags --libs gtk-4-fortran))。四 多平台构建与持续集成
fpm run/ctest 或 make test,对比数值结果与性能回归。fpm test 或 ctest,收集覆盖率与性能基准。五 调试与交付
gdb ./hello;单元测试建议纳入 fpm test 或 CTest,保证每次提交均可回归。-fPIC -c 与 -shared 生成 .so,.NET 侧 [DllImport] 导入)。