Unidata / netcdf-fortran

Official GitHub repository for netCDF-Fortran libraries, which depend on the netCDF C library. Install the netCDF C library first.
Other
236 stars 95 forks source link

Problem configuring NetCDF-Fortran with Cmake and DCMAKE_INSTALL_PREFIX option #301

Open barriern opened 3 years ago

barriern commented 3 years ago

Hi everyone.

I have a problem when trying to build netcdf-fortran using CMake. I use the following command:

cmake -DCMAKE_Fortran_COMPILER=mpif90 -DCMAKE_CXX_COMPILER=mpicxx -DCMAKE_C_COMPILER=mpicc -DCMAKE_INSTALL_PREFIX=${PWD}/install -DCMAKE_PREFIX_PATH="/home/barrier/Libs/netcdf_par/git-netcdf-c/build/install" ..

which returns the following error:

-- Looking for nc_get_chunk_cache_ints in netCDF::netcdf
-- Looking for nc_get_chunk_cache_ints in netCDF::netcdf - not found
-- Looking for nccreate in netCDF::netcdf
-- Looking for nccreate in netCDF::netcdf - not found
-- Looking for nc_set_log_level in netCDF::netcdf
-- Looking for nc_set_log_level in netCDF::netcdf - not found
-- Looking for oc_open in netCDF::netcdf
-- Looking for oc_open in netCDF::netcdf - not found
-- Performing Test HAVE_SZIP_WRITE
-- Performing Test HAVE_SZIP_WRITE - Failed
-- Looking for nc_def_var_szip in netCDF::netcdf
-- Looking for nc_def_var_szip in netCDF::netcdf - not found
CMake Error at CMakeLists.txt:643 (message):
  netcdf-c version 4.7.4 or greater is required

However, when I use:

cmake -DCMAKE_Fortran_COMPILER=mpif90 -DCMAKE_CXX_COMPILER=mpicxx -DCMAKE_C_COMPILER=mpicc -DCMAKE_INSTALL_PREFIX=${PWD}/install -DnetCDF_LIBRARIES="/home/barrier/Libs/netcdf_par/git-netcdf-c/build/install/lib/libnetcdf.so" -DnetCDF_INCLUDE_DIR="/home/barrier/Libs/netcdf_par/git-netcdf-c/build/install/include" ..

It works perfecly.

The nc-config command points to the right path:

barrier@MPLCLTPO0218:~/Libs/netcdf_par/git-netcdf-fortran/build$ which nc-config 
/home/barrier/Libs/netcdf_par/git-netcdf-c/build/install/bin/nc-config

Could you please tell me what I am missing?

Here are my environment settings:

Thanks a lot.

Nicolas

adamcpovey commented 2 years ago

I have had an equivalent issue compiling v4.5.3, receiving the following error when I use CMAKE_PREFIX_PATH

CMake Error at /data/ifort_scratch/netcdf-fortran/build/CMakeFiles/CMakeTmp/CMakeLists.txt:18 (add_executable):
  Target "cmTC_ea817" links to target "hdf5::hdf5_hl-shared" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?

CMake Error at /data/ifort_scratch/netcdf-fortran/build/CMakeFiles/CMakeTmp/CMakeLists.txt:18 (add_executable):
  Target "cmTC_ea817" links to target "hdf5::hdf5-shared" but the target was
  not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?

CMake Error at /network/software/ubuntu_bionic/cmake/3.22/share/cmake-3.22/Modules/CheckLibraryExists.cmake:72 (try_compile):
  Failed to generate test project build system.
Call Stack (most recent call first):
  CMakeLists.txt:624 (CHECK_LIBRARY_EXISTS)

It works fine if I switch to netCDF_LIBRARIES and netCDF_INCLUDE_DIR

This happens with ifort 18.0.5 and gfortran 7.5.0 with cmake 3.22 on Ubunutu 18.04.

adamcpovey commented 2 years ago

Just confirming that this is still an issue for v4.9.0, with cmake ignoring the prefix provided and linking against system versions of the libraries (I'm at a university and don't have root access). I can compile using the following,

cmake -DCMAKE_PREFIX_PATH="$LIB_DIR" \
      -DnetCDF_LIBRARIES="$LIB_DIR/lib/libnetcdf.so" \
      -DnetCDF_INCLUDE_DIR="$LIB_DIR/include" \
      -DCMAKE_BUILD_TYPE="Release" \
      -DBUILD_SHARED_LIBS=ON \
      -DNC_EXTRA_DEPS=jpeg \
      -DCMAKE_INSTALL_PREFIX="$LIB_DIR" \
      -G "Unix Makefiles" -Wno-dev ..
WardF commented 2 years ago

Interesting, thank you for raising the profile of this, again. I'm preparing the 4.6.0 release now and will see if I can figure out what's going on. There's really not an obvious reason that it's doing this. My guess is that there is some subtlety to cmake that I'm unaware of. Out of curiosity, what version of cmake is being used? I'm sure it's a modern version, but the more data points, the better.

adamcpovey commented 2 years ago

I've tried cmake 3.22.0 (installed natively) and 3.23.2 (installed within a conda environment).