This article edescribes how to use the netcdf 4.1 libraries in Compaq Visual Fortran (CVF) on a windows platform.
A working Fortran example (work space + project space + compiled modules) is provided at https://svn.oss.deltares.nl/repos/openearthtools/trunk/fortran/io/netcdf/ (registration required).
Below follows a stepwise explanation on how to obtain this working example.
- Download the precompiled NetCDF for Intel and Portland Group Fortran compilers libraries (binaries) from http://www.unidata.ucar.edu/software/netcdf/docs/faq.html#windows_netcdf4.
- Unzip and add these to your project folder . For this example we put the libraries in folder
lib\win32\vs\pg
- Adapt your project file either via the user interface of by manually editing the *.dsp file. You need to adapt the following
Table compiler flags for external interfaces
*.dsp line
description
/names:lowercase
argument passing conventions
/assume:underscore
external name interpretation
/iface:nomixed_str_len_arg
string length argument passing
These options fit with the exact name of functions as present in the precompiled unidata netcdf binaries.
Different compilers result in different settings. You might be able to get other versions of the binaries working by modifying these settings. We found these settings by trial and error when implementing netcdf into XBeach, revision 1103). You an also specify these settings in the user interface.
- Optionally add the option to add preprocessor flags. This allows you to flags pieces of netcdf-related code like
module ncoutput_module #ifdef USENETCDF #define NC_ERROR_CHECK=if (status) write(*,*) nf90_message(status) use netcdf status = nf90_create(... NC_ERROR_CHECK status = nf90_addvar(... status = nf90_putvar(... #endif
Table compiler flags for preprocessor*.dsp line
description
/fpp
argument passing conventions
/define:"DLL_NETCDF"
allow string "DLL_NETCDFF" to be used in pre processor code
/define:"USENETCDF"
allow string "USENETCDF" to be used in pre processor code
You can also specify these settings in the user interface.
- The two bullets change the default dsp file line
into
# ADD F90 /compile_only /nologo /warn:nofileopt
# ADD F90 /compile_only /nologo /warn:nofileopt /assume:underscore /iface:nomixed_str_len_arg /names:lowercase /fpp /define:"DLL_NETCDF" /define:"USENETCDF"
- Link the netcdf libraries: add
netcdf.lib
and/libpath:"lib\win32\vs\pg"
, which is the direcory where we unzipped the unidata *.zip file.
This changes the default dsp file lineinto# ADD LINK32 kernel32.lib ... odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib ... odbccp32.lib netcdf.lib /nologo /subsystem:console /machine:I386 /libpath:"lib\win32\vs\pg"
- Running an executable with netcdf requires presence of the dll's i) next to the executable or ii) in a folder that that is in your system
PATH
. You can add a macro to your *.dsp file that copies the requires libraries from the unzipped unidata folder to your executable folder. Adding the libraries to a system path is not recommended, as it might result into conflict with other versions of the netcdf libraries.# Begin Special Build Tool OutDir=.\netcdf_unidata_only___Win32_Release SOURCE="$(InputPath)" PostBuild_Cmds=copy lib\win32\vs\pg\*.dll $(OutDir) copy lib\win32\all\hdf5\dll\*.dll $(OutDir) copy lib\win32\all\szip\dll\*.dll $(OutDir) # End Special Build Tool