Calling NAG Fortran Library Routines
from C/C++ Language Programs Using the NAG C Header File
Shah Datardina and Ian Hounam
NAG Ltd, Oxford
© The Numerical Algorithms Group Ltd, Oxford UK. 2009
A header file containing the function prototypes can be included in the user's program to allow the C/C++ compiler to check argument passage. Such a header file (nagmk22.h) has been created for the current NAG Fortran Library. This document explains how to call Fortran routines from C and C++ using the NAG header file on Windows and Linux/Unix Systems. Current users of NAG header files may prefer to skip to Section 8.
Generally under Windows, names are converted to upper case. However with some compilers, e.g. the Intel Fortran compiler on 32-bit Windows with the option /iface:cvf an additional qualifier is attached to the subprogram name, i.e., X01AAF becomes __stdcall X01AAF. This is the standard calling convention adopted by Microsoft for the WIN32 API.
DOUBLE PRECISION D double d;
INTEGER I int i;
LOGICAL L int l;
CHARACTER*n S char *s; int len_s; /* See below */
COMPLEX*16 Z struct {double re,im;} z;
In C all scalar arguments are passed as pointers to a variable; this means that a constant must first be assigned to a variable and then the address of the variable passed.
On the other hand in C++, scalar arguments may be passed as references with the result that all scalar arguments become C++ reference arguments. This C Header File caters for C++ compilers and uses reference arguments. Hence the "address of" operator must not be used for these arguments. In user supplied functions dereferencing such arguments is also avoided. Also constants may be passed to such arguments.
Character arguments are passed as two arguments
All Linux/Unix Fortran compilers append this argument to the end of the argument list. Under Windows, the Intel Fortran compiler places the length at the end of the argument list by default. However, with the /iface:cvf option, the length of the character array is passed immediately after the string pointer, i.e., the compiler option /iface:mixed_str_len_arg is turned on. Note also that with /iface:cvf the standard calling convention (__stdcall) is enforced.
NB: On HP-UX 64, the type of the length parameter is long.
For example to call a routine called "NAGSUB" which in Fortran looks like this
SUBROUTINE NAGSUB(A,B,C)
CHARACTER A
CHARACTER*3 B
CHARACTER*5 C(2)
With most Unix and Linux compilers the C code looks like this.
extern void nagsub_(char* a, char *b, char c[],
int len_a, int len_b, int len_c);
main()
{
char a = 'a';
int len_a = 1;
char b[] = "abc";
int len_b = 3;
char c[2][6] = {"abcde", "fghij"}; /* Note the value 6 to allow
for the null termination character */
int len_c = 5;
nagsub_(&a, b, (char *)c, len_a, len_b, len_c);
}
The C code looks like this when the Fortran code is compiled with 32-bit Windows Intel Fortran using the option /iface:cvf
extern void __stdcall NAGSUB(char* a, int len_a,
char *b, int len_b, char c[], int len_c);
main()
{
char a = 'a';
int len_a = 1;
char b[] = "abc";
int len_b = 3;
char c[2][6] = {"abcde", "fghij"}; /* Note the value 6 to allow
for the null termination character */
int len_c = 5;
NAGSUB(&a, len_a, b, len_b, (char *)c, len_c);
}
Note that the declared length of char variables in the C program must allow space for the null termination in the C string.
The Fortran type COMPLEX*16 (or COMPLEX(KIND=0.0d0)) is provided in the NAG header files by the typedef "Complex", which expands to "struct {double re,im;}"
Fortran functions will map as shown in the table above for data types, except for COMPLEX*16 functions and CHARACTER functions (see below).
Procedure arguments, i.e. function or subroutine names, are passed by address in the normal C manner.
COMPLEX*16 FUNCTION F(X)
COMPLEX*16 X
has the following prototype in C:
Complex (*f)(Complex *)and a user callable function, e.g S01EAF, looks like this
extern Complex s01eaf_(const Complex *z,int *ifail);
The prototype for the user supplied function F with these compilers is the following.
extern void f(Complex *, Complex *);
and a user callable function, e.g S01EAF, looks like this
extern void s01eaf_(Complex *return_value, const Complex *z,
int *ifail);
One solution is to use the NAG Fortran Compiler implementation of the NAG Fortran Library, if available, please see above.
As it is impossible to access the return value of a COMPLEX*16 FUNCTION from C, another solution is to write a Fortran "jacket" routine to convert the COMPLEX*16 FUNCTION to a SUBROUTINE with an extra initial argument containing a pointer to the return value. The "jacket" routine then calls S01EAF.
For example to call S01EAF, write a jacket routine called S01EAFJ which would have the following prototype.
extern void s01eafj_(Complex *ret_val, CONST Complex *z,The jacket routine is then called from the C program rather than S01EAF.
int *ifail);
SUBROUTINE S01EAFJ(RET_VAL, Z, IFAIL)This routine can be compiled and linked with the C files and NAG Fortran Library in the normal way.
COMPLEX*16 RET_VAL, Z, S01EAF
INTEGER IFAIL
RET_VAL = S01EAF(Z, IFAIL)
END
Similar jacket routines can be written for user supplied functions.
The supplied C Header file does not contain prototypes for these jacket functions. You may need to modify these prototypes appropriately.
e.g.
CHARACTER*10 FUNCTION F(I)is called thus:
extern void f_(char *,int,int *);
char c[10];
int clen = 10,i;
f_(&c,clen,&i)
The HP-UX 64-bit compiler uses long instead of int for the hidden length argument.
As Fortran stores multi-dimension arrays in column major order whereas C/C++ store in row major order, either
int array[] /* 2 dimensions */
and for 3 dimensions:
double array[] /* 3 dimensions */
The code examples in this section are for the Intel Fortran Compiler on 32-bit Windows with the /iface:cvf option. On other systems the __stdcall attribute should be omitted.
The prototype for a hypothetical NAG Fortran routine with a 2 dimensional DOUBLE PRECISION array argument would look like this:
extern void __stdcall NAGSUB(double[] /* 2 dimensions */);
A simple program to call this routine might look like this:
main ()
{
double p[2][2];
NAGSUB((double *)p);
}
Note that we need to cast the 2 dimensional C array actual argument to (double *).
The example prototype below shows how to call a hypothetical NAG routine that takes a single subroutine argument. This user supplied subroutine takes a 2 dimension DOUBLE PRECISION array and an integer which specifies the leading dimension of the Fortran array.
extern void __stdcall NAGSUB(void (*f) (double[] /* 2 dimensions */,
int *));
The C code for the user supplied function is listed below. The 2 dimension array is passed as a pointer to double and the code must carry out array indexing using the dimension information passed from Fortran. In this case, the macro P uses the leading dimension of the Fortran array, which is the trailing dimension of the C array, to index into the array p. The array p is only referenced through this macro.
void __stdcall fun(double p[], int *tdp)
{
#define P(I,J) p[(I)*(*tdp) + (J)]
P(0,0) = 0.0;
P(1,1) = 1.0;
}
The main function looks like this:
main ()
{
void __stdcall fun(double p[], int *tdp);
NAGSUB(fun);
}
Example 2 below shows a complete program that illustrates these concepts.
C/C++ users on Windows 64, Linux and Solaris 64 can use the header file as is.
Users on 32-bit Windows using the NAG Fortran library compiled with Intel Fortran using the /iface:cvf option will need to provide the option -DUSE_STDCALL to their C/C++ compiler in order to use the prototypes in which character lengths follow the character arguments and to activate the __stdcall attribute.
Users on systems where a complex function returns a complex structure again need do nothing. For compilers such as Sun SPARC Solaris 32-bit, the compiler option -DRETURN_COMPLEX_PARAMETER will have to be specified.
Users with the 64-bit HP-UX compiler need to edit the header file to change the typedef of Charlen to long.
The C Header files contain an 'extern "C"' declaration for C++ compilers, i.e.
#ifdef __cplusplus(with a matching "}" at the end of the file). As described in section 3, scalar arguments are passed by reference, so it follows that C++ users must not supply the address of scalar variables.
extern "C" {
#endif