Previous: Errors, Up: Use of the NetCDF Library


1.6 Compiling and Linking with 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, where the netCDF library and include files are installed, and whether or not you are using shared libraries. 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.

If the netCDF library was configured with the –enable-shared flag, and the operating system supports shared libraries, then it should be possible to link an application program using a relatively simple command. For example:

     cc -o myprogram myprogram.o -L/usr/local/netcdf/lib -lnetcdf

It should be noted that on some operating systems, when using shared libraries, the application itself may need to be compiled using some form of PIC (position independent code) flag; the particular flag will depend on the C compiler used. You should try it first without any PIC flag, and if that fails, then check with the system administrator about the proper form of PIC flag to use.

In addition, for some C compilers (e.g. Sun's cc compiler) it is necessary to specify runtime paths to the relevant libnetcdf.so. This can be accomplished in one of two ways.

  1. Add the path to the directory containing libnetcdf.so to the LD_LIBRARY_PATH environment variable. This path is searched at runtime to locate any needed shared library. This might be accomplished, for example, by the following shell command (assuming that libnetcdf.so is in /usr/local/netcdf/lib).
              LD_LIBRARY_PATH="/usr/local/netcdf/lib:$LD_LIBRARY_PATH"
              export LD_LIBRARY_PATH
    
  2. Set the so-called runtime path when the application is linked so that the absolute paths of all needed shared libraries is included in the application binary. For gcc under Linus, this is usually automatic. For C compilers on Solaris (and probably other operating systems) the runtime path must be specified at link time. The command in this case might look like this.
              cc -o myprogram myprogram.o -L/usr/local/netcdf/lib -lnetcdf -R/usr/local/netcdf/lib
    

    Note that the -R flag is also C compiler dependent. For gcc and Linux, for example, the specification is usually of this form.

              cc ...  -Wl,-rpath,/usr/local/netcdf/lib
    

    Other compilers may use other flags to specify this. Check with the local system administrator.

If shared libraries are not supported or are not being used for some reason, then it is necessary to include all the dependent libraries in the compile command. For example, for a netCDF-4 enabled library, it will be necessary to link with two HDF5 libraries, at least one compression library, and (on some systems) the math library.

     cc -o myprogram myprogram.o -L/usr/local/netcdf/lib -L/usr/local/hdf5/lib -lnetcdf -lhdf5_hl -lhdf5 -lz

Other configuration features (e.g. DAP support or parallel IO) may require additional libraries.

A complete list of necessary libraries can be obtained by executing the “nc-config –libs” command. For example:

     ./nc-config --libs

might return something like this:

     -L/tmp/install/spock/lib -lnetcdf -L/upc/share/stdinstall/local/spock/lib
     -lhdf5_hl -lhdf5 -L/upc/share/stdinstall/local/spock/lib -lz -lm
     -L/upc/share/stdinstall/local/spock/lib -lcurl -L/usr/kerberos/lib64
     -L/upc/share/stdinstall/local/spock/lib
     -lidn -lssl -lcrypto -lldap -lrt -lssl -lcrypto -ldl -lz -lz

Obviously there is some redundancy in this list, so it can be reduced somewhat to produce this slightly simpler list.

     -L/tmp/install/spock/lib -lnetcdf
     -L/upc/share/stdinstall/local/spock/lib -lhdf5 -lhdf5_hl -lz -lcurl
     -L/usr/kerberos/lib64 -lcrypto -lssl
     -ldl -lidn -lldap -lm -lrt