出现错误:-bash: make: command not found
原因分析:一般出现这个-bash: make: command not found提示,是因为安装系统的时候使用的是最小化mini安装,系统没有安装make、vim等常用命令,直接sudo apt-get install安装下即可。
解决方法:
sudo apt-get install ksh
LDFLAGS = -O3 -fconvert=big-endian -mcmodel=large -ffloat-store -mieee-fp -march=native -W -L/usr/local/netcdf4/lib -lnetcdff -lnetcdf
出现错误:
/usr/bin/ld: /home/jiang/Desktop/programs/4AOP…/lib.gfortran/lib/mainaaaa.a(uwiremis_read_db.o):in function ‘uwiremis_read_db_’:umiremis_read_db.f90:(.text+0x36):undefined reference to ‘ncdinq_’
如果 nm -D /usr/local/netcdf4/lib/libnetcdff.so | grep ncdinq_
没有输出,说明 libnetcdff.so
中确实不包含 ncdinq_
这个符号。这通常有以下几种可能原因:
ncdinq_
未找到 的主要原因是:- NetCDF-Fortran 未正确安装 → 安装
libnetcdff-dev
。 - 库路径错误 → 使用
nf-config --flibs
或find
查找正确路径。 - API 版本不匹配 → 确保代码和库版本一致(NetCDF3/4)。
- Fortran 符号命名问题 → 检查
nm -D
输出并调整FFLAGS
。
- NetCDF-Fortran 未正确安装 → 安装
可能的原因和解决方案
1. NetCDF-Fortran 库未正确安装
- 你可能只安装了 NetCDF-C(
libnetcdf.so
),但没有安装 NetCDF-Fortran(libnetcdff.so
)。 - 或者安装的 NetCDF-Fortran 版本不包含
ncdinq_
(可能是旧版或编译选项错误)。
解决方法:
- 检查是否安装了
libnetcdff
:apt list --installed | grep netcdf # Ubuntu/Debian yum list installed | grep netcdf # CentOS/RHEL
- 如果没有安装,重新安装 NetCDF-Fortran:
sudo apt install libnetcdff-dev # Ubuntu/Debian sudo yum install netcdf-fortran-devel # CentOS/RHEL
- 如果是手动编译安装的,确保编译时启用了 Fortran 支持:
./configure --enable-fortran make && make install
2. NetCDF 版本问题
ncdinq_
是 NetCDF3 的 Fortran API,而 NetCDF4 可能使用了不同的接口(如nf90_inquire
)。- 如果你的代码是旧版 NetCDF3 风格,但链接的是 NetCDF4,可能会导致符号缺失。
解决方法:
- 检查代码是否使用的是 NetCDF3 API(如
ncdinq
)或 NetCDF4 API(如nf90_inquire
)。 - 如果是 NetCDF3 风格,尝试:
- 安装 NetCDF3 兼容版本:
sudo apt install libnetcdf-dev libnetcdff-dev # 确保安装 Fortran 支持
- 或者在编译时指定 兼容模式:
LIBS = -lnetcdff -lnetcdf -DNETCDF3 # 如果代码支持宏切换
- 安装 NetCDF3 兼容版本:
3. 库路径错误
-L/usr/local/netcdf4/lib
可能不是 NetCDF-Fortran 的真实路径。- 系统可能默认使用
/usr/lib
或/usr/lib/x86_64-linux-gnu/
下的库。
解决方法:
- 查找正确的
libnetcdff.so
路径:find /usr -name "libnetcdff.so*"
- 如果找到在其他路径(如
/usr/lib/x86_64-linux-gnu/
),更新LDFLAGS
:LDFLAGS = -O3 ... -L/usr/lib/x86_64-linux-gnu -lnetcdff -lnetcdf
4. Fortran 符号命名问题
- Fortran 编译器可能会修改函数名(如
ncdinq
→ncdinq_
或ncdinq__
)。 - 如果库里的符号是
ncdinq
而代码调用ncdinq_
,会导致链接失败。
解决方法:
- 查看库中的实际符号名:
nm -D /usr/local/netcdf4/lib/libnetcdff.so | grep -i ncdinq
- 如果输出是
ncdinq
(无下划线),需要在编译时调整命名风格:FFLAGS += -fno-underscoring # 禁止自动添加下划线
- 如果输出是
ncdinq_
,但代码调用ncdinq
,则需要统一命名方式。
- 如果输出是
5. 使用 nf-config
自动获取正确的链接选项
- 如果
nf-config
可用,直接使用它来获取正确的库路径和依赖项:LIBS = $(shell nf-config --flibs)
- 示例输出:
这样可以避免手动指定路径错误。-L/usr/lib -lnetcdff -lnetcdf -lhdf5_hl -lhdf5 -lz -lm
最终建议的修改
1. 确保 NetCDF-Fortran 已正确安装
sudo apt install libnetcdff-dev # Debian/Ubuntu
sudo yum install netcdf-fortran-devel # CentOS/RHEL
2. 更新 Makefile
# 使用 nf-config 自动获取库路径(推荐)
LIBS = $(shell nf-config --flibs)
# 如果没有 nf-config,手动指定(示例)
# LIBS = -L/usr/lib/x86_64-linux-gnu -lnetcdff -lnetcdf -lhdf5_hl -lhdf5 -lz -lm
# 编译选项
FFLAGS = -O3 -fconvert=big-endian -mcmodel=large -ffloat-store -mieee-fp -march=native
# 链接规则
your_program: your_code.o
gfortran $(FFLAGS) -o $@ $^ $(LIBS)
3. 检查代码是否使用正确的 API
- 如果是 NetCDF3 代码,确保链接的是 NetCDF3 兼容库。
- 如果是 NetCDF4 代码,建议更新到
nf90_*
接口。
总结
如果仍然有问题,请提供:
nf-config --flibs
的输出(如果有)。find /usr -name "libnetcdff.so*"
的结果。- 你的代码中是如何调用
ncdinq
的(是否用了use netcdf
?)。
重要提示:如果未设置参数IDCOMPIL,编译过程将使用默认文件Makefile.4a.inc的配置,编译结果将放在library/lib/目录中,而不是library/lib$IDCOMPIL/目录中。在这种情况下,编译可能会因错误而停止,因为默认配置可能与当前系统和编译器不兼容。