This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
Jed- I have modified the netcdf-c documentation to try to elaborate on the linking issue. I have included it below. Comments welcome. =Dennis Heimbigner ...Every C file that references netCDF functions or constants must contain an appropriate #include statement before the first such reference: @example #include <netcdf.h> @end example 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: @example cc -c -I/usr/local/netcdf/include myprogram.c @end example 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: @example cc -o myprogram myprogram.o -L/usr/local/netcdf/lib -lnetcdf @end example It should be noted that on many operating systems, when using shared libraries, the application itself must be compiled using the -fPIC or -PIC flag (depending on the C compiler). In addition, for some operating systems (e.g. Solaris), it is necessary to specify runtime paths to the relevant libnetcdf.so. This can be accomplished in one of two ways. @enumerate @item 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). @example LD_LIBRARY_PATH="/usr/local/netcdf/lib:$LD_LIBRARY_PATH" export LD_LIBRARY_PATH @end example @item 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 Linux, this is usually automatic. For Solaris (and probably other operating systems) the runtime path must be specified at link time. The Solaris command would look like this. @example cc -o myprogram myprogram.o -L/usr/local/netcdf/lib -lnetcdf -R/usr/local/netcdf/lib @end example @end enumerate 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: @example cc -o myprogram myprogram.o -L/usr/local/netcdf/lib -L/usr/local/hdf5/lib -lnetcdf -lhdf5_hl -lhdf5 -lz @end example A complete list of necessary libraries can be obtained by executing the ``nc-config --libs'' command. For example: @example ./nc-config --libs @end example might return something like this: @example -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 @end example Obviously there is some redundancy in this list, so it can be reduced somewhat to produce this slightly simpler list. @example -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 @end example Ticket Details =================== Ticket ID: NBS-392373 Department: Support netCDF Priority: High Status: Open