Previous: Errors, Up: Use of the NetCDF Library
Details of how to compile and link a program that uses the netCDF C or FORTRAN interfaces differ, depending on the operating system, the available compilers, and where the netCDF library and include files are installed. Nevertheless, we provide here examples of how to compile and link a program that uses the netCDF library on a Unix platform, so that you can adjust these examples to fit your installation.
Every FORTRAN file that references netCDF functions or constants must contain an appropriate INCLUDE statement before the first such reference:
INCLUDE 'netcdf.inc'
Unless the netcdf.inc file is installed in a standard directory where the FORTRAN compiler always looks, you must use the -I option when invoking the compiler, to specify a directory where netcdf.inc is installed, for example:
f77 -c -I/usr/local/netcdf/include myprogram.f
Alternatively, you could specify an absolute path name in the INCLUDE statement, but then your program would not compile on another platform where netCDF is installed in a different location.
In all netCDF versions before 3.6.2, the Fortran 77, Fortran 90, and the C libraries were all built into the same library file. That is, the libnetcdf.a file contains the C functions, the F77 functions, and the F90 functions. (The C++ library is separate.)
Starting with version 3.6.2, another method of building the netCDF fortran libraries becomes available. With the –enable-separate-fortran option to configure, the user can specify that the C library should not contain the fortran functions. In these cases an additional library, libnetcdff.a (not the extra “f”) will be built. This library contains the fortran functions.
For more information about configure options, See Specifying the Environment for Building.
Building separate fortran libraries is required for shared library builds, but is not done, by default, for static library builds.
When linking fortran programs without a separate fortran library, programs must link to the netCDF library like this:
f77 -o myprogram myprogram.o -L/usr/local/netcdf/lib -lnetcdf
Alternatively, you could specify an absolute path name for the library:
f77 -o myprogram myprogram.o -l/usr/local/netcdf/lib/libnetcdf.
When linking fortran programs with separate fortran, the user must link to both the fortran and the C libraries.
f77 -o myprogram myprogram.o -L/usr/local/netcdf/lib -lnetcdff -lnetcdf
Unless the netCDF library (or libraries) is installed in a standard directory where the linker always looks, you must use the -L and -l options to link an object file that uses the netCDF library.