ROMS NetCDF linking error / OSX / M1

Discussion on computers, ROMS installation and compiling

Moderators: arango, robertson

Post Reply
Message
Author
stephenlynch
Posts: 2
Joined: Mon Mar 04, 2024 7:09 pm
Location: MITRE Labs

ROMS NetCDF linking error / OSX / M1

#1 Unread post by stephenlynch »

Hi,
My guess is this is a simple fix that will seem obvious, but I've been unable to resolve it after spending some time and effort at it. Help is very much appreciated!
As the subject indicates, having a hard time getting through the linking stage, although preprocessing and compiling go fine.
I'm new to ROMS, and trying to build on OSX (14.3.1) where I've installed netcdf, netcdf-fortran, hdf5, perl, etc using homebrew.
Trying to compile using cbuild_roms.sh with gfortran compiler. Defined netcdf lib and inc directories as environment variables in .bash_profile, and ROMS_ROOT_DIR in .zshrc.
USE_NETCDF4 and USE_HDF5 are both set to ON. (piecing together lessons from various places in this forum).
A hint occurs early that netcdf will raise issues when cmake / make only end up specifying the netcdf lib. Output from the build script:

Code: Select all

--  netcdf_libs = netcdf
-- netcdf_ldirs = /opt/homebrew/lib
--  netcdf_idir = /opt/homebrew/include
However, in a terminal nf-config yields:

Code: Select all

This  4.6.1 has been built with the following features: 

  --cc        -> clang
  --cflags    -> -I/opt/homebrew/Cellar/netcdf-fortran/4.6.1/include  -g -Wall -Wno-unused-variable -Wno-unused-parameter -O2 

  --fc        -> /opt/homebrew/bin/gfortran
  --fflags    -> -I/opt/homebrew/Cellar/netcdf-fortran/4.6.1/include -I/opt/homebrew/Cellar/netcdf-fortran/4.6.1/include
  --flibs     -> -L/opt/homebrew/Cellar/netcdf-fortran/4.6.1/lib -lnetcdff -lnetcdf -lnetcdf 
  --has-f90   -> TRUE
  --has-f03   -> yes

  --has-nc2   -> yes
  --has-nc4   -> yes

  --prefix    -> /opt/homebrew/Cellar/netcdf-fortran/4.6.1
  --includedir-> /opt/homebrew/Cellar/netcdf-fortran/4.6.1/include
  --version   ->  4.6.1
After digging around and leaving breadcrumbs in various places it's apparent the Darwin-gfortran.mk config file (nor any of the .mk files, so far as I can tell) is being included, and NF_CONFIG is never being defined (and nf-config never being called during build), so it's falling back on just libnetcdf, which I think would imply a NetCDF3 build?
Again, help is appreciated. Hopefully I've provided enough information to get to an answer easily -- apologies if I've left anything out, and happy to address quickly.
-Stephen

robertson
Site Admin
Posts: 227
Joined: Wed Feb 26, 2003 3:12 pm
Location: IMCS, Rutgers University

Re: ROMS NetCDF linking error / OSX / M1

#2 Unread post by robertson »

Setting netcdf lib and inc dirs as environment variables is really only for very old NetCDF versions where the fortran and c functions all reside in a single libnetcdf.a file and nf-config does not exits. What you want to do is set the environment variable NF_CONFIG to nf-config since that executable is in your path. If you later build your own netcdf that is in a special location then NF_CONFIG should be set to the full path to the nf-config executable (e.g. export NF_CONFIG=/home/joeroms/software/netcdf-fortran/bin/nf-config).

Setting up your ROMS_ROOT_DIR and setting USE_NETCDF4 to on are correct. However, setting USE_HDF5 to on is unnecessary unless you are coupling ROMS to a model that needs just HDF5 without the NetCDF-4 layer.

stephenlynch
Posts: 2
Joined: Mon Mar 04, 2024 7:09 pm
Location: MITRE Labs

Re: ROMS NetCDF linking error / OSX / M1

#3 Unread post by stephenlynch »

HI,

Ah hah! :D That makes a lot of sense, and have made changes accordingly. Thank you!

However, while it's now finding nf-config and using its outputs, I'm still getting a puzzling linking error:

Code: Select all

[100%] Linking Fortran executable romsM
ld: library not found for -lnetcdf
collect2: error: ld returned 1 exit status
make[2]: *** [romsM] Error 1
make[1]: *** [CMakeFiles/romsM.dir/all] Error 2
make: *** [all] Error 2
[  0%] Generating fixdep
[  0%] Built target fix
[ 99%] Built target Objects
[ 99%] Built target ROMS_static
[100%] Linking Fortran executable romsM
ld: library not found for -lnetcdf
collect2: error: ld returned 1 exit status
make[2]: *** [romsM] Error 1
make[1]: *** [CMakeFiles/romsM.dir/all] Error 2
make: *** [all] Error 2

I do also notice that nf-config returns -lnetcdf twice, and wonder if that's normal / problematic (output in original post).

Thanks again!

robertson
Site Admin
Posts: 227
Joined: Wed Feb 26, 2003 3:12 pm
Location: IMCS, Rutgers University

Re: ROMS NetCDF linking error / OSX / M1

#4 Unread post by robertson »

I use macports instead of homebrew. It appears from the output you shared above that homebrew puts the the netcdf-c libraries in a different directory than netcdf-fortran. There are several ways to fix this, the easiest is take the -L part of the output of 'nc-config --libs' and add a LIBS =+ line to your Darwin-gfortran.mk in the USE_NETCDF4 section at line 198. The output will likely be something like -L/opt/homebrew/Cellar/netcdf-c/4.9.2/lib -lnetcdf so in Darwin-gfortran.mk, change:

Code: Select all

ifdef USE_NETCDF4
        NF_CONFIG ?= nf-config
    NETCDF_INCDIR ?= $(shell $(NF_CONFIG) --prefix)/include
             LIBS += $(shell $(NF_CONFIG) --flibs)
           INCDIR += $(NETCDF_INCDIR) $(INCDIR)
else
to

Code: Select all

ifdef USE_NETCDF4
        NF_CONFIG ?= nf-config
    NETCDF_INCDIR ?= $(shell $(NF_CONFIG) --prefix)/include
             LIBS += -L/opt/homebrew/Cellar/netcdf-c/4.9.2/lib
             LIBS += $(shell $(NF_CONFIG) --flibs)
           INCDIR += $(NETCDF_INCDIR) $(INCDIR)
else
Ideally you would copy the contents of the Compilers directory into ~/Compilers/ROMS (or similar), make your changes to Darwin-gfortran.mk, and change the COMPILERS variable in your build_roms.sh to point to this new directory. i.e. change:

Code: Select all

 export         COMPILERS=${MY_ROMS_SRC}/Compilers
#export         COMPILERS=${HOME}/Compilers/ROMS
to

Code: Select all

#export         COMPILERS=${MY_ROMS_SRC}/Compilers
 export         COMPILERS=${HOME}/Compilers/ROMS
We will have a more generic fix for this situation soon.

robertson
Site Admin
Posts: 227
Joined: Wed Feb 26, 2003 3:12 pm
Location: IMCS, Rutgers University

Re: ROMS NetCDF linking error / OSX / M1

#5 Unread post by robertson »

I just remembered that you are using cbuild_roms.sh and not build_roms.sh. I also checked the brew formula for netcdf and netcdf-fotran and neither is keg-only so there should be symlinks for libnetcdf.dylib and libnetcdff.dylib in ${HOMEBREW_PREFIX}/lib (probably /opt/homebrew/lib) if libnetcdf and libnetcdff are not in there then try running:

Code: Select all

brew link netcdf
brew link netcdf-fortran
I don't use homebrew but from what I'm reading that should create the proper symlinks so the linker can find them.

Post Reply