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 C file that references netCDF functions or constants must contain an appropriate #include statement before the first such reference:
#include <netcdf.h>
Unless the netcdf.h file is installed in a standard directory where the C compiler always looks, you must use the -I option when invoking the compiler, to specify a directory where netcdf.h is installed, for example:
cc -c -I/usr/local/netcdf/include myprogram.c
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.
Unless the netCDF library 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. For example:
cc -o myprogram myprogram.o -L/usr/local/netcdf/lib -lnetcdf
Alternatively, you could specify an absolute path name for the library:
cc -o myprogram myprogram.o -l/usr/local/netcdf/lib/libnetcdf.a