2

Hi everybody and thanks to your help,

I'm newbie of CMAKE and I try to write a find library for C and Fortran NETCDF library. The problem is the possible different location and version of fortran and C netcdf.

For fortran library I search netcdf.mod(include module) and libnetcdff.so (dynamic library) instead for C library I search netcdf.h and libnetcdf.so.

On the web I find a magic code that find correctly the netcdf C library :

find_path (NETCDF_INCLUDES_C netcdf.h HINTS NETCDF_DIR NETCDF_DIR)
message(" inc =  ${NETCDF_INCLUDES_C}  ")
find_library (NETCDF_LIBRARIES_C  NAMES netcdf)
message(" lib =  ${NETCDF_LIBRARIES_C}  ")

and the two path is correct. I try to reply the code for fortran:

find_path (NETCDFF_INCLUDES_F90 netcdff.mod HINTS NETCDFF_DIR NETCDFF_DIR)
message(" inc ff =  ${NETCDFF_INCLUDES_F90}  ")
find_library (NETCDFF_LIBRARIES_F90  NAMES netcdff HINTS NETCDFF_DIR )
message(" lib ff=  ${NETCDFF_LIBRARIES_F90}  ")
mark_as_advanced(NETCDF_LIBRARIES_F90)

but the both F90 path is empty, probably because I don't understand how cmake find the C library. Through MODULE ENVIRONMENT i load both library and the module load that env var:

   $NETCDFF_HOME $NETCDFF_INC $NETCDFF_INCLUDE $NETCDFF_LIB
   $NETCDF_HOME  $NETCDF_INC  $NETCDF_INCLUDE  $NETCDF_LIB 

I suppose incorrectly that CMAKE search NETCDF_DIR variable and into this search netcdf.h but in my CmakeList and in my ENV that variable is not defined.

How CMake find C var? and how I can reply the find to fortran lib?

Thanks to help me

Best regards

Eric

5
  • 1
    Did you really mean netcdff.mod or did you want netcdf.mod? Also I don't really know cmake but if looks like you might be trying to store in NETCDFF_INCLUDES_F90 but then your message uses ${NETCDF_INCLUDES_F90} (i.e. there's an extra F in the first) Commented Jan 10, 2017 at 14:34
  • thanks to your reply, in copy and paste there are happens some mistakes. The correct include module file is netcdf.mod and message is on NETCDFF_INCLUDES_F90, I correct my previous post. Commented Jan 10, 2017 at 14:39
  • which version of CMake are you running? Commented Jan 10, 2017 at 15:24
  • I think you've forgotten to correct the module file name. Commented Jan 10, 2017 at 15:41
  • The CMAKE version is 3.5.2, the correct name of module is netcdf.mod. Commented Jan 10, 2017 at 15:50

1 Answer 1

3

Maybe mine is not an answer per se, but couldn't you just use the FindNetCDF.cmake shipped with VTK?
https://github.com/Kitware/VTK/blob/master/CMake/FindNetCDF.cmake
Or the one from other users which look similar to the previous one (they can look for Fortran and C components)?
https://github.com/bilke/cmake-modules/blob/master/FindNetCDF.cmake
https://github.com/jedbrown/cmake-modules/blob/master/FindNetCDF.cmake
If you include this file then you could just retrieve the components you need via:

set (NETCDF_F90 "YES")
find_package (NetCDF REQUIRED)
include_directories(${NETCDF_INCLUDES})
target_link_libraries (uses_f90_interface ${NETCDF_LIBRARIES})
target_link_libraries (only_uses_c_interface ${NETCDF_LIBRARIES_C}) 

or in the case of the VTK version something like

find_package (NetCDF COMPONENTS F90)
include_directories(NETCDF_F90_INCLUDE_DIRS)
target_link_libraries (only_uses_f90_interface ${NETCDF_F90_LIBRARIES})

As stated by VTK version, you can pass different search directories for C and Fortran libraries via the following:

When interfaces are requested the user has access to interface specific hints:

NETCDF_${LANG}_INCLUDE_DIR - where to search for interface header files
NETCDF_${LANG}_LIBRARY     - where to search for interface libraries
Sign up to request clarification or add additional context in comments.

4 Comments

I have tried the example but all run only if C and Fortran lib are in the same directory. Because for the first thing the script find the location of C libs and after search the other in the same directory.
updated answer, since you can specify different search directories for includes and libraries when you ask for language specific interfaces...
Thanks to the help I try your suggestion!
There are many different ways that netCDF can be built and autoconf/automake/libtool have absolutely no problem finding all the info they need. However cmake struggles to find even the basic information, because apparently it does not use the .so file (where all the information can be found) but instead merely hopes that netCDF was also installed with cmake, so that the special cmake install info is present. But this is usually not the case. So in other projects, I also struggle to put netCDF support into cmake builds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.