Compute Canada

How do I use the NAG C Library?

Please note: The FAQ pages at the HPCVL website are continuously being revised. Some pages might pertain to an older configuration of the system. Please let us know if you encounter problems or inaccuracies, and we will correct the entries.

The C Library is used by directly linking the static or dynamic versions of the library to the program that uses it. Other libraries should be used in conjunction with it, such as socket or intl. The library resides in /opt/nag/clsol07dal. The following lines are typical for a compilation with the NAG libraries:


cc -c -dalign -I/opt/nag/clsol08dcl/include program.c

Linking (static):

cc -o program.exe -dalign -lm -lsocket -lnsl -lintl -xlic_lib=sunperf -lsunmath \
program.o /opt/nag/clsol08dcl/lib/libnagc_spl.a

Linking (dynamic):

cc -o program.exe -dalign -lm -lsocket -lnsl -lintl -xlic_lib=sunperf -lsunmath program.o \
-Bdynamic -R/opt/nag/clsol08dcl/lib -L/opt/nag/clsol08dcl/lib -lnagc_spl

This is assuming that you want to use the Sun Performance Libraries from within the NAG routines. If you prefer the generic routines (making the code slower, but more portable), replace the reference to nagc_spl bynagc_nag and drop the -xlic_lib=sunperf option.

Note that the -dalign option is necessary to avoid bus errors. The nsl, intl, and socket libraries are necessary only in some cases, but it is recommended to include them in the link list. Make sure that cc actually refers to Sun native C-compiler and not to another compiler that might reside on the system and be prefered by yourPATH setting. You can check by typing "which cc" which should yield the response "/opt/studio12/SUNWspro/bin/cc" or similar.

The NAG C-Library requires some programming conventions that are best determined from the documentation. Header files need to be included, and as seen in the example above, the -I option should be used to point the compiler to the directory where they reside (/opt/nag/clsol08dcl/include). Several of the routines that areSUBROUTINEs in the Fortran version of the NAG library are functions in the C-version. The names may differ as well. A simple example is

a02cdc divides one complex number x=(a,b) by another y=(c,d) to yield a third z=(e,f)

The involved numbers need to be declared Complex in the C program, which requires the header files nag.h and naga02.h to be included.

#include #include #include #include int main(void) ... double a,b,c,d,e,f;
Complex x,y,z; ... x=a02bac(a,b); y=a02bac(c,d) z=a02cdc(x,y); ...

In this case, we used the routine a02bac to assign real and imaginary parts to a Complex number, and the convention and to extract the real and imaginary parts from a Complex number (which is a structurein C).

For details about the usage of specific program, consult the programming examples in/opt/nag/clsol08dcl/examples/source in conjunction with the documentation.