jedbrown / cmake-modules

CMake modules for some scientific libraries
BSD 2-Clause "Simplified" License
87 stars 54 forks source link

FindNetCDF finds libnetcdf but not netcdf.mod #21

Open rouson opened 7 years ago

rouson commented 7 years ago

Pleas let me know if you can offer any advice on resolving the following problem:

I set up a small repository to learn how to use FindNetCDF. The repository has just two Fortran source files, only one of which depends on NetCDF as specified in a single use netcdf statement. An abbreviated transcript of my results using my repository's add-cmake-file branch follows:

$ git clone --recursive https://github.com/sourceryinstitute/learning-cmake-find.git
$ cd learning-cmake-find
$ git checkout add-cmake-file
$ mkdir build
$ cd build 
$ FC=gfortran cmake ..

-- Found NetCDF: /opt/local/lib/libnetcdf.dylib

$ make
Scanning dependencies of target io_routines
[ 50%] Building Fortran object
CMakeFiles/io_routines.dir/src/io_routines.f90.o
/Users/rouson/Code/learning-make-find/src/io_routines.f90:18:8:

     use netcdf
        1
Fatal Error: Can't open module file ‘netcdf.mod’ for reading at (1): No
such file or directory
compilation terminated.
make[2]: *** [CMakeFiles/io_routines.dir/src/io_routines.f90.o] Error 1
make[1]: *** [CMakeFiles/io_routines.dir/all] Error 2
make: *** [all] Error 2

I used MacPorts on macOS Sierra to install netcdf and netcdf-fortran with the resulting installed files:

$ port contents netcdf
Port netcdf contains:
  /opt/local/bin/nc-config
  /opt/local/bin/nccopy
  /opt/local/bin/ncdump
  /opt/local/bin/ncgen
  /opt/local/bin/ncgen3
  /opt/local/include/netcdf.h
  /opt/local/include/netcdf_mem.h
  /opt/local/include/netcdf_meta.h
  /opt/local/lib/cmake/netCDF/netCDFConfig.cmake
  /opt/local/lib/cmake/netCDF/netCDFConfigVersion.cmake
  /opt/local/lib/cmake/netCDF/netCDFTargets-release.cmake
  /opt/local/lib/cmake/netCDF/netCDFTargets.cmake
  /opt/local/lib/libnetcdf.11.3.0.dylib
  /opt/local/lib/libnetcdf.11.dylib
  /opt/local/lib/libnetcdf.dylib
  /opt/local/lib/libnetcdf.settings
  /opt/local/lib/pkgconfig/netcdf.pc
  /opt/local/share/man/man1/nccopy.1.gz
  /opt/local/share/man/man1/ncdump.1.gz
  /opt/local/share/man/man1/ncgen.1.gz
  /opt/local/share/man/man1/ncgen3.1.gz
  /opt/local/share/man/man3/netcdf.3.gz

$ port contents netcdf-fortran
Port netcdf-fortran contains:
  /opt/local/bin/nf-config
  /opt/local/include/netcdf.inc
  /opt/local/include/netcdf.mod
  /opt/local/include/netcdf4_f03.mod
  /opt/local/include/netcdf4_nc_interfaces.mod
  /opt/local/include/netcdf4_nf_interfaces.mod
  /opt/local/include/netcdf_f03.mod
  /opt/local/include/netcdf_fortv2_c_interfaces.mod
  /opt/local/include/netcdf_nc_data.mod
  /opt/local/include/netcdf_nc_interfaces.mod
  /opt/local/include/netcdf_nf_data.mod
  /opt/local/include/netcdf_nf_interfaces.mod
  /opt/local/include/typesizes.mod
  /opt/local/lib/libnetcdff.6.dylib
  /opt/local/lib/libnetcdff.a
  /opt/local/lib/libnetcdff.dylib
  /opt/local/lib/pkgconfig/netcdf-fortran.pc
  /opt/local/share/man/man3/netcdf_fortran.3.gz
jedbrown commented 7 years ago

You could try pkg-config, which is probably a better option these days. I haven't tested FindNetCDF.cmake in quite a while (and no longer use CMake in any of my projects). I don't think it was ever tested for a Fortran project. I'm sorry, but I don't have time to add this feature or help you debug at this time. I'd be happy to merge a reasonable patch if you learn how to do it.

You might solve your problem merely by telling your Fortran compiler about NETCDF_INCLUDES.

rouson commented 7 years ago

Thanks for the quick reply. I'll let you know once it's sorted out. If I don't sort it out on my own, I'll ask for help from Kitware, Inc., on CMake.

zbeekman commented 7 years ago

Hi Damian,

You need to add the NetCDF includes:

cmake_minimum_required(VERSION 3.7)

project(build_against_netcdf LANGUAGES Fortran)

# Find the native NetCDF includes and library
include(cmake-modules/FindNetCDF.cmake)

set (NETCDF_F90 "YES") # Require Fortran interfaces to be FOUND
find_package (NetCDF REQUIRED)

include_directories(${NETCDF_INCLUDES})

add_library(io_routines src/io_routines.f90)

target_link_libraries (io_routines ${NETCDF_LIBRARIES})

A really good way to see what facilities a find module provides is to run ccmake or cmake-gui and then toggle the advanced options. In ccmake t toggles the advanced options. Ctrl-n is go to the next line, Ctrl-p is go to the previous line, c is configure (i.e. run cmake), g is configure, generate and quit:

asciicast