Windows Intel Fortran编译netCDF-Fortran

Windows Intel Fortran编译netCDF-Fortran

步骤

  1. 下载安装预编译的netCDF-C https://docs.unidata.ucar.edu/netcdf-c/current/winbin.html 假设安装在了C:\Libs\netCDF-c\4.9.1

  2. 在netCDF-Fortran的GitHub主页下载最新的Release https://github.com/Unidata/netcdf-fortran/releases/tag/v4.6.0

  3. 解压源代码到C:\Libs\netCDF-fortran\source

  4. 从开始菜单打开Intel oneAPI command prompt for Intel 64 for Visual Studio 2022

  5. 使用cmake生成项目。注意cmake参数中路径分隔符不要用反斜杠\
    假设我们要安装到C:/Libs/netCDF-fortran/Debug目录下

    cd C:\Libs\netCDF-fortran\
    cmake -S source -B build-debug -DnetCDF_LIBRARIES=C:/Libs/netCDF-c/4.9.1/lib/netcdf.lib -DnetCDF_INCLUDE_DIR=C:/Libs/netCDF-c/4.9.1/include -DCMAKE_INSTALL_PREFIX=C:/Libs/netCDF-fortran/Debug
    
  6. 编译生成netcdff

    cmake --build build-debug --target netcdff netcdff_c --config Debug
    
  7. 手动生成netcdff.lib

    cd build-debug\fortran
    lib netcdff.dir\Debug\*.obj netcdff_c.dir\Debug\netcdff_c.lib /out:Debug\netcdff.lib
    
  8. 编译生成测试。正常情况下,生成失败数应该是0。

    cd ..\..\
    cmake --build build-debug --target ALL_BUILD --config Debug
    
  9. 执行安装命令,会安装到第5步指定的-DCMAKE_INSTALL_PREFIX=目录下

    cmake --build build-debug --target INSTALL --config Debug
    

总结

  • 第五步的指定netCDF_LIBRARIESnetCDF_INCLUDE_DIR很关键,不然cmake找不到netcdf-c,会报错netcdf-c version 4.7.4 or greater is required

  • 第7步的手动生成netcdff.lib很关键。应该是项目的bug,编译netcdff时不会在Debug目录中生成netcdff.lib,会导致最终安装的文件没有该链接库文件,并且测试项目也不会通过(因为需要链接这个库文件,会报错fatal error LNK1104: 无法打开文件“..\fortran\Debug\netcdff.lib”

  • cmake默认编译的是Debug版本,Release版本的编译方法如下(不再详细解释)

    cd C:\Libs\netCDF-fortran\
    
    cmake -S source -B build-release  -DCMAKE_BUILD_TYPE:STRING=RELEASE -DnetCDF_LIBRARIES=C:/Libs/netCDF-c/4.9.1/lib/netcdf.lib -DnetCDF_INCLUDE_DIR=C:/Libs/netCDF/4.9.1/include -DCMAKE_INSTALL_PREFIX=C:/Libs/netCDF-fortran/Release
    
    cmake --build build-release --target netcdff netcdff_c --config Release
    
    cd build-release\fortran
    lib netcdff.dir\Release\*.obj netcdff_c.dir\Release\netcdff_c.lib /out:Release\netcdff.lib
    
    cd ..\..\
    cmake --build build-release --target ALL_BUILD --config Release
    cmake --build build-release --target INSTALL --config Release
    

    测试

在Visual Studio 新建一个fortran项目

  • 项目->属性->Fortran->Additional Include Directories,添加C:\Libs\netcdf-fortran\Debug\inlude\Debug
  • 项目->属性->Linker->Additional Library Directories,添加C:\Libs\netCDF-fortran\Debug\libC:\Libs\netCDF-c\4.9.1\lib
  • 项目->属性->Linker->Input->Additional Dependencies,填写netcdff.lib netcdf.lib
PROGRAM testnc
USE netcdf
IMPLICIT NONE
INTEGER :: ncid, status
CHARACTER(len=*),parameter:: ncfile = "C:\Datasets\OSTIA\202202\20220201120000-UKMO-L4_GHRSST-SSTfnd-OSTIA-GLOB-v02.0-fv02.0.nc"

status = nf90_open(ncfile, nf90_nowrite, ncid)

call list_all_var(ncid)
END PROGRAM

SUBROUTINE list_all_var(fid)
USE netcdf
IMPLICIT NONE
INTEGER, Intent(IN) :: fid
INTEGER             :: nVars, res, varid
CHARACTER(len=20)::var_name
res = nf90_inquire(fid, nVariables=nvars)
PRINT *,"所有变量:"
DO varid=1, nVars
    res= nf90_inquire_variable(fid,varid,name=var_name)
    print *, varid,". ",var_name
END DO
END SUBROUTINE

这个方案时使用intel x64 的终端编译的,使用时一定要注意这里的x64,
在这里插入图片描述

  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 18
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值