schism-dev / schism

Semi-implicit Cross-scale Hydroscience Integrated System Model (SCHISM)
http://ccrm.vims.edu/schismweb/
Apache License 2.0
78 stars 84 forks source link

Cmake FindNetCDF reports conflicting library when anaconda/conda is installed #53

Open jamal919 opened 2 years ago

jamal919 commented 2 years ago

Hi all,

I was trying to build SCHISM with cmake build system in a new Ubuntu machine (Ubuntu 20.04.3 LTS). I have installed necessary packages through apt - compilers, netcdf (c and fortran), mpich, cmake etc. I have also anaconda installed on my computer in home directory ~/.anaconda3.

If I issue the cmake configure command cmake -C ../cmake/SCHISM.local.build -C ../cmake/SCHISM.local.ubuntu ../src -DUSE_WWM=on, the output has something weird - it picks up the fortran netcdf fine, but links to the netcdf-c library inside ~/.anaconda

-- FindNetCDF defines targets:
--   - NetCDF_VERSION [4.7.3]
--   - NetCDF_PARALLEL [FALSE]
--   - NetCDF_C_CONFIG_EXECUTABLE [/usr/bin/nc-config]
--   - NetCDF::NetCDF_C [SHARED] [Root: /usr] Lib: /home/khan/.anaconda3/lib/libnetcdf.so
--   - NetCDF_Fortran_CONFIG_EXECUTABLE [/usr/bin/nf-config]
--   - NetCDF::NetCDF_Fortran [SHARED] [Root: /usr] Lib: /usr/lib/x86_64-linux-gnu/libnetcdff.so

...

### Configuring Utilities
-- In /Utility NetCDF_LIBS /home/khan/.anaconda3/lib/libnetcdf.so;/usr/lib/x86_64-linux-gnu/libnetcdff.so
-- In /Utility NetCDF_LIBS /home/khan/.anaconda3/lib/libnetcdf.so;/usr/lib/x86_64-linux-gnu/libnetcdff.so

Then afterwards, if I build schism, the executables are created fine! But they fail during execution - whenever NetCDF is involved.

My experience with cmake is rather short. I tried passing the NetCDF_ROOT variable to FindNetCDF.cmake through -DNetCDF_ROOT flag, but no avail.

Any fix for this issue? (Other than, of course, editing CMakeCache.txt by hand).

Thanks.

Edit: The problem is reproduced in multiple machines with different software configurations, but with existence of anaconda in home directory.

jamal919 commented 2 years ago

For now, I am avoiding such mismatch by explicitly defining NetCDF_C environment variable, either by

  1. bash export export NetCDF_C=/usr/lib/x86_64-linux-gnu/
  2. cmake flag cmake -C ../cmake/SCHISM.local.build -C ../cmake/SCHISM.local.ubuntu ../src -DNetCDF_C=/usr/lib/x86_64-linux-gnu/

Thanks.

platipodium commented 2 years ago

Dear Jamal, thanks for reporting this. I am not an export in CMake either but I have encountered problems with multiple netcdf libs installed system-wide and through conda persistently. So thank you for your fix.

You may also move the -DNetCDF_C=/usr/lib/x86_64-linux-gnu/ into a CMake.local definition file for your system in the cmake subdirectory

platipodium commented 2 years ago

Here's the CMakeCache.txt from my system with macports and anaconda installed. Same problem

//NetCDF nc-config helper
NetCDF_C_CONFIG_EXECUTABLE:FILEPATH=/opt/local/bin/nc-config

//NetCDF C include directory
NetCDF_C_INCLUDE_FILE:FILEPATH=/opt/local/include/netcdf.h

//NetCDF C library
NetCDF_C_LIBRARY:STRING=/opt/homebrew/anaconda3/lib/libnetcdf.dylib

So the nc-config and --includedir are correctly assigned, but the library is taken from anaconda; also the same for the Fortran part.

Setting -DNetCDF_C did not remedy this.

jamal919 commented 2 years ago

Hi Carsten,

Have you tried a fresh build directory. I remember that the first time I was trying I already had CMakeCache.txt with old values and setting -DNetCDF_C did not help. Then after cleaning the build directory (rm -rf) the values were properly set. Could be a solution for you.

I have no experience with mac system, and no way to reproduce this behaviour either in similar environment.

jamal919 commented 2 years ago

Hi Carsten @platipodium,

Did you finally found a way to make the cmake to pick up NetCDF correctly? I am just curious based on your recent commit ebb27b60621e9dd4c83f87f11621f15dfd97fd30

platipodium commented 2 years ago

Works for me, but only after removing conda from my PATH (despite the hints given in the cmake config files)