This article describes how to use the netcdf 4.1 libraries in the Compaq Visual Fortran (CVF) 6.6(C) on a windows platform. Note that CVF is superseded in favor of the Intel Fortran compilers. Before you read this you might want to consider migrating to Intel compiler. This page is meant for those who want to use netcdf now but do not want to migrate compilers now now (no time to spare now, or perhaps due to the license fee of a new intel fortran compiler). 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) (registration required). 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
Code Block 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 above change the default *.dsp file line
# ADD F90 /compile_only /nologo /warn:nofileopt
intoCode Block # 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 folder where we unzipped the unidata *.zip file. This changes the default dsp file line# ADD LINK32 kernel32.lib ... odbccp32.lib /nologo /subsystem:console /machine:I386
intoCode Block # 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 required 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. For instance, note that here we deal with netcdf 4.1 for fortran, whereas the latest netcdf is already at version 4.2.Code Block # 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