错误:对NetCDF函数的未定义引用

时间:2016-10-18 09:19:10

标签: linux fortran linker-errors gfortran netcdf

背景

我正致力于编译MCIP,这意味着centos 5.9系统中的气象化学接口处理器。

我使用gcc -version 4.9来实现这个过程。

设置

以下是〜/ .bashrc中的一些配置设置:

export DIR=/disk2/hyf/lib ## All lib ar installed under this path
# NetCDF setting
export PATH="$DIR/netcdf/bin:$PATH"
export NETCDF="$DIR/netcdf"
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$NETCDF/lib     
# IOAPI 
export BIN=Linux2_x86_64gfort
export BASEDIR=/disk2/hyf/backup/software/ioapi
export PATH=$DIR/ioapi-3.1/bin:$PATH
export LD_LIBRARY_PATH=$DIR/ioapi-3.1/lib:$LD_LIBRARY_PATH
# Set M3LIB for the model
export M3LIB=/disk2/hyf/cmaq/CMAQv5.1/lib

我也为CMAQ模型做软链接,因为这些:
    ln -s $ NETCDF $(M3LIB)/ x86_64 / gcc / netcdf     ln -s $ IOAPI $(M3LIB)/ x86_64 / gcc / ioapi

生成文件

以下是 Makefile 中的一些子程序:

 # Requirements: set M3LIB before running this script

.SUFFIXES:
.SUFFIXES: .o .f90 .F90

MODEL = mcip.exe

#...gfortran
FC      = gfortran
NETCDF = $(M3LIB)/netcdf
IOAPI_ROOT = $(M3LIB)/ioapi
FFLAGS  = -O3 -gdwarf-2 -gstrict-dwarf -I$(NETCDF)/include -   I$(IOAPI_ROOT)/include \
      -ffpe-trap='invalid','zero','overflow','underflow'
##FFLAGS        = -g -O0  \
      -ffpe-trap='invalid','zero','overflow','underflow'  \
      -I$(NETCDF)/include -I$(IOAPI_ROOT)/include
LIBS    = -L$(IOAPI_ROOT)/lib -lioapi  \
      -L$(NETCDF)/lib -lnetcdf -lgomp

DEFS   =

MODULES =\
 const_mod.o \
 const_pbl_mod.o \
 coord_mod.o \
 date_time_mod.o \
 date_pack_mod.o \
 files_mod.o \
 groutcom_mod.o \
 luvars_mod.o \
 mcipparm_mod.o \
 mcoutcom_mod.o \
 mdoutcom_mod.o \
 metinfo_mod.o \
 metvars_mod.o \
 vgrd_mod.o \
 wrf_netcdf_mod.o \
 xvars_mod.o \
 sat2mcip_mod.o

OBJS =\
 mcip.o \
 alloc_ctm.o \
 alloc_met.o \
 alloc_x.o \
 bcldprc_ak.o \
 blddesc.o \
 chkwpshdr.o \
 chkwrfhdr.o \
 close_files.o \
 collapx.o \
 comheader.o \
 cori.o \
 dealloc_ctm.o \
 dealloc_met.o \
 dealloc_x.o \
 detangle_soil_px.o \
 e_aerk.o \
 dynflds.o \
 getgist.o \
 getluse.o \
 getmet.o \
 getpblht.o \
 getsdt.o \
 getversion.o \
 graceful_stop.o \
 gridout.o \
 init_io.o \
 init_met.o \
 init_x.o \
 julian.o \
 layht.o \
 ll2xy_lam.o \


 .......

错误

制作后的输出显示如下:

make[1]: Entering directory `/disk2/hyf/cmaq/CMAQv5.1/scripts/mcip/src'
gfortran -g -O0 -gdwarf-2 -gstrict-dwarf \ 
         -I/disk2/hyf/cmaq/CMAQv5.1/lib/x86_64/gcc/netcdf/include \      
         -I/disk2/hyf/cmaq/CMAQv5.1/lib/x86_64/gcc/ioapi/include  -c const_mod.f90
......

chkwpshdr.o: In function `chkwpshdr_':
/disk2/hyf/cmaq/CMAQv5.1/scripts/mcip/src/chkwpshdr.f90:109: \    
 undefined reference to `__netcdf_MOD_nf90_get_att_one_fourbyteint'

 (a lot of these code showing the same mistake 'undefined reference')  

 /disk2/hyf/cmaq/CMAQv5.1/lib/x86_64/gcc/ioapi/lib/libioapi.a(open3.o): In    
 function `open3_':
 open3.F:(.text+0x1531): undefined reference to `ncclos_'
 .........

我认为编译器在某些情况下可能与.F和.f90文件有一些冲突。但我不知道为什么。 gcc已经成功安装,并定义了$ PATH。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我必须通过将-lnetcdff和-lnetcdf(按此顺序)添加到MCIP makefile中的LIBS选项来解决它。确保NETCDF变量指向系统中安装netcdf的正确路径。