检查 NetCDF Fortran的版本
执行
nf-config --all
命令后,它会输出一堆信息,大致像这样:
This netCDF-Fortran version: 4.6.0
netCDF-Fortran installation dir: /usr/local/netcdf4
Fortran compiler: gfortran
Fortran compiler flags: -g -O2
Fortran preprocessor flags: -I/usr/local/netcdf4/include
Fortran linker flags: -L/usr/local/netcdf4/lib
Fortran libraries: -lnetcdff -lnetcdf
This netCDF-Fortran 4.4.0 has been built with the following features:
This netCDF-Fortran 4.4.0 has been built with the following features: --cc -> gcc--cflags -> -I/usr/local/netcdf4/include -I/usr/local/netcdf4/include--fc -> gfortran--fflags -> -I/usr/local/netcdf4/include--flibs -> -L/usr/local/netcdf4/lib -lnetcdff -L/usr/local/netcdf4/lib -lnetcdf -lnetcdf --has-f90 -> no--has-f03 -> yes--has-nc2 -> yes--has-nc4 -> yes--prefix -> /usr/local/netcdf4--includedir-> /usr/local/netcdf4/include--version -> netCDF-Fortran 4.4.0
这里面你关心的几行是:
行名 | 说明 |
---|---|
Fortran preprocessor flags | 包含头文件用的路径,比如 -I/usr/local/netcdf4/include |
Fortran linker flags | 链接库文件的路径,比如 -L/usr/local/netcdf4/lib |
Fortran libraries | 需要链接的库名,比如 -lnetcdff -lnetcdf |
简单总结:
- 头文件路径 →
Fortran preprocessor flags
- 库文件路径 →
Fortran linker flags
- 需要链接的库 →
Fortran libraries
所以如果你想手动写Makefile或者编译命令,大概就这么用:
gfortran your_code.f90 -I/usr/local/netcdf4/include -L/usr/local/netcdf4/lib -lnetcdff -lnetcdf
好的,你的 nf-config --all
输出是这样的👇,我给你逐行详细解释:
项目 | 含义 | 你的值 |
---|---|---|
This netCDF-Fortran 4.4.0 has been built with the following features: | netCDF-Fortran 版本信息。 | 你用的是 4.4.0 版 |
--cc | 用来编译 C 语言部分的编译器。 | gcc |
--cflags | C 编译器的编译选项(主要是头文件路径)。 | -I/usr/local/netcdf4/include -I/usr/local/netcdf4/include (写了两次,应该是安装时的小问题,但一般没大碍) |
--fc | 用来编译 Fortran 代码的编译器。 | gfortran |
--fflags | Fortran 编译选项(主要是头文件路径)。 | -I/usr/local/netcdf4/include |
--flibs | Fortran链接时要加的库和路径。 | -L/usr/local/netcdf4/lib -lnetcdff -L/usr/local/netcdf4/lib -lnetcdf -lnetcdf (注意这里链接了两次 -lnetcdf ,有点多余,但不影响) |
--has-f90 | 是否支持 Fortran 90 风格接口(模块 use netcdf )。 | no (不支持 F90 风格接口!) |
--has-f03 | 是否支持 Fortran 2003 风格接口(use netcdf 模块等新标准)。 | yes (支持 F2003 风格接口!) |
--has-nc2 | 是否支持 netCDF-2 格式文件(老格式)。 | yes |
--has-nc4 | 是否支持 netCDF-4(HDF5后端格式)。 | yes |
--prefix | 安装路径的前缀。 | /usr/local/netcdf4 |
--includedir | 头文件所在的目录。 | /usr/local/netcdf4/include |
--version | 再次确认版本。 | netCDF-Fortran 4.4.0 |
重点提醒你⚡:
-
--has-f90 -> no
- 表示 不支持 Fortran 90 传统模块接口(比如
use netcdf
)。 - 但你的代码里有可能用到了 Fortran 77接口,比如
ncdinq_
这样的下划线形式。 - 所以你需要用
-lnetcdff
,而且要正确链接。
- 表示 不支持 Fortran 90 传统模块接口(比如
-
--flibs
告诉你,编译时要加:-L/usr/local/netcdf4/lib -lnetcdff -lnetcdf
小总结:
你的编译链接命令应该长这样:
gfortran your_code.f90 -I/usr/local/netcdf4/include -L/usr/local/netcdf4/lib -lnetcdff -lnetcdf
-I/usr/local/netcdf4/include
→ 找.mod
文件(虽然你的版本没F90模块,但保持这样没问题)-L/usr/local/netcdf4/lib
→ 找.so
或.a
静态库-lnetcdff -lnetcdf
→ 链接 fortran接口库 + netcdf主库
为什么你遇到 undefined reference to 'ncdinq_'
?
原因就是:你在链接的时候,要确保加上 -lnetcdff
,因为 ncdinq_
是 netcdff
里面提供的函数。
如果忘了或者顺序不对,链接器就找不到了!
要不要我帮你写一个完整示例(包括编译和链接的命令行)?
告诉我你用的是单个 .f90
文件,还是有一堆 .o
和 .a
文件,我可以具体写一版给你用🔧!要吗?🚀