Astro's 踩坑记录

Ubuntu 20.04下 netcdf库的安装

字数统计: 296阅读时长: 1 min
2022/03/28

针对ifort或是gfortran编译器
首先切换root用户

环境变量设置
对于ifort
1
2
3
4
5
6
7
source /opt/intel/oneapi/setvars.sh intel64    # 来自intel官方安装说明 将ifort命令添加到环境变量中
export NETCDF=/usr/local/netcdf-ifort   #-------------->所有库将被安装到这个路径中
export CC=icc
export CXX=icpc
export FC=ifort
export F90=ifort
export F77=ifort
对于gfortran
1
2
3
4
5
export F77=gfortran
export FC=gfortran
export CC=gcc
export CXX=g++
export CFLAGS=-fPIC

安装netcdf库所需依赖
ZLIB
1
2
3
4
5
cd zlib-1.2.8/
./configure --prefix=${NETCDF}
make
make check
make install
hdf5
1
2
3
4
5
cd ../hdf5-1.10.5/
./configure --with-zlib=${NETCDF} --prefix=${NETCDF}
make
make check
make install
netcdf-c
1
2
3
4
5
cd ../netcdf-c*
CPPFLAGS=-I${NETCDF}/include LDFLAGS=-L${NETCDF}/lib ./configure --prefix=${NETCDF} --enable-netcdf-4 --enable-largefile --disable-dap
make
make check
make install
netcdf-fortran
1
2
3
4
5
6
cd ../netcdf-fortran*
export LD_LIBRARY_PATH=${NETCDF}/lib:${LD_LIBRARY_PATH}
CPPFLAGS=-I${NETCDF}/include LDFLAGS=-L${NETCDF}/lib  ./configure --prefix=${NETCDF} --disable-fortran-type-check
make
make check
make install

安装完成

将以下部分添加到~/.bashrc文件中并测试

1
2
3
4
5
6
7
# netcdf
export NETCDF=/usr/local/netcdf-ifort
export PATH=${NETCDF}/bin:${PATH}
export LD_LIBRARY_PATH=${NETCDF}/lib:${LD_LIBRARY_PATH}
export CPPFLAGS=-I${NETCDF}/include
export LDFLAGS=-L${NETCDF}/lib
echo "*********netcdf for ifort activated*********"

以下是测试是否安装成功的方法

1
2
ifort -o a.out xxx.F90 -I${NETCDF}/include -L${NETCDF}/lib -lnetcdff  -lnetcdf
./a.out
CATALOG
  1. 1. 环境变量设置
    1. 1.1. 对于ifort
    2. 1.2. 对于gfortran
  2. 2. 安装netcdf库所需依赖
    1. 2.1. ZLIB
    2. 2.2. hdf5
    3. 2.3. netcdf-c
    4. 2.4. netcdf-fortran
  3. 3. 安装完成