# NAG C Header Files FAQ

 Q1. Under Unix, why do I get unresolved external references when I try to link my program? Q2. When I call a NAG routine with a multi-dimension array I get the wrong answer, what am I doing wrong? Q3. Can I use the C Header Files from C++? Q4. Do I need a Fortran compiler if I want to call the NAG Fortran Library from C?

## Q1. Under Unix, why do I get unresolved external references when I try to link my program?

The NAG Fortran Library must be linked with the Fortran run time libraries. This is most easily achieved by linking using the f95 command to link your previously compiled C object files with the NAG Fortran Library. In this way the f95 compiler driver transparently links the Fortran run time libraries for you, e.g.

cc -c myprog.c

f95 -o myprog myprog.o -lnag


This is the advice that we give in the technical documentation that accompanies the C Header Files and is intended to be general. You do need to link in the Fortran run time libraries in some way. If you know what these are, you can certainly use cc or ld to do this. Indeed in some special cases it may be desirable to use cc or ld for the linking.

One way to discover what Fortran run time libraries are normally linked by the f95 command is to invoke f95 with an option to print out the commands generated for each pass of the compiler. This might be -dryrun, -v or -V. Look at the man page for f95 on your system to find out. (You may need to create a dummy file consisting of, for example, a single end statement and specify this to f95.) Look at the ld command generated and try to work out the -L and -l commands necessary to pick up the libraries. It may help to create a dummy C program and print the cc compiler passes in similar way.

To link Fortran code using the cc command: the -L and -l options that are in f95's ld command but not in cc's ld command must be specified to cc.

To link Fortran code using the ld command: the -L and -l options from f95's ld command and cc's ld command must be merged.

In either case some experimentation may be necessary to get the order correct.

Unless it is absolutely necessary to do otherwise use f95 to link the program, it's so much easier!

## Q2. When I call a NAG routine with a multi-dimension array I get the wrong answer, what am I doing wrong?

A common source of problem with multi-dimension arrays is the different order in which Fortran and C store the dimensions.

Fortran stores multi-dimension arrays in column major order whereas C stores in row major order, so either

• the C routine must store and manipulate the transpose of the problem matrix, or
• the C routine must transpose the matrix before and after calling the Fortran routine.

## Q3. Can I use the C Header Files from C++?

Yes. You can use the NAG C Header Files with C++. These header files contain the following lines near the beginning:

#ifdef __cplusplus

extern "C" {

#endif



and a corresponding } near the end:

#ifdef __cplusplus

}

#endif



This declares all the Fortran routines to be C functions and not subject to inheritance etc.

## Q4. Do I need a Fortran compiler if I want to call the NAG Fortran Library from C?

The NAG Fortran Library makes use of Fortran intrinsic functions and I/O routines, therefore you require the appropriate Fortran run time libraries. Depending on the implementation, these may be shipped with the NAG Fortran Library distribution, but if they aren't you will require the appropriate Fortran compiler.