Sample ILC applications
Fortran main program that calls a C++ function:
@PROCESS LIST
PROGRAM CEFOR2C
* Module/File Name: AFHCFOR
***************************************************************
* FUNCTION : Interlanguage communications call to a *
* a C program. *
* *
* This example illustrates an interlanguage call from *
* a Fortran main program to a C function. *
* The parameters passed across the call from Fortran *
* to C have the following declarations: *
* *
* Fortran INTEGER*2 to C short as pointer *
* Fortran INTEGER*4 to C int *
* Fortran REAL*4 to C float *
* Fortran REAL*8 to C double *
* Fortran CHARACTER*23 to C as pointer to pointer to CHAR *
***************************************************************
***************************************************************
* DECLARATIONS OF VARIABLES FOR THE CALL TO C *
***************************************************************
INTEGER*4 J
EXTERNAL CECFFOR
INTEGER*4 CECFFOR
INTEGER*2 FOR_SHORT / 15 /
INTEGER*4 FOR_INT / 31 /
REAL*4 FOR_FLOAT / 53.99999 /
REAL*8 FOR_DOUBLE / 3.14159265358979312D0 /
POINTER*4 (FOR_POINTER, CHAR_POINTEE)
CHARACTER*23 CHARSTRING /'PASSED CHARACTER STRING'/
CHARACTER*23 CHAR_POINTEE
**************************************************************
* PROCESS STARTS HERE *
**************************************************************
PRINT *, '*********************************'
PRINT *, 'FORTRAN CALLING C EXAMPLE STARTED'
PRINT *, '*********************************'
FOR_POINTER = LOC(CHARSTRING)
PRINT *, 'CALLING C FUNCTION'
J = CECFFOR( LOC(FOR_SHORT), FOR_INT, FOR_FLOAT,
1 FOR_DOUBLE, LOC(FOR_POINTER))
PRINT *, 'RETURNED FROM C FUNCTION'
IF (J /= 999) THEN
PRINT *, 'ERROR IN RETURN CODE FROM C'
ENDIF
PRINT *, '*******************************'
PRINT *, 'FORTRAN CALLING C EXAMPLE ENDED'
PRINT *, '*******************************'
END /*Module/File Name: EDCCFOR */
Cfunction invoked by a Fortran program:
#pragma linkage (CECFFOR,FORTRAN)
#include <stdio.h>
#include <string.h>
#include <math.h>
/************************************************************
* This is an example of a C function invoked by a Fortran *
* program. *
* CECFFOR is called from Fortran program CEFOR2C with the *
* following list of arguments: *
* Fortran INTEGER*2 to C short as pointer *
* Fortran INTEGER*4 to C int *
* Fortran REAL*4 to C float *
* Fortran REAL*8 to C double *
* Fortran CHARACTER*23 to C as pointer to pointer to char *
************************************************************/
int CECFFOR (short **c_short,
int *c_int,
float *c_float,
double *c_double,
char *** c_character_string
)
{
int ret=999; /* Fortran program expects 999 returned */
fprintf(stderr,"CECFFOR STARTED\n");
/***********************************************************
* Compare each passed argument against the C value. *
* Issue an error message for any incorrectly passed *
* parameter. *
***********************************************************/
if (**c_short != 15)
{
fprintf(stderr,"**c_short not = 15\n");
--ret;
}
if (*c_int != 31)
{
fprintf(stderr,"*c_int not = 31\n");
--ret;
}
if (fabs(53.99999 - *c_float) > 1.0E-5F)
{
fprintf(stderr,
"fabs(53.99999 - *c_float) > 1.0E-5F, %f\n", *c_float);
--ret;
}
if (fabs(3.14159265358979312 - *c_double) > 1.0E-13)
{
fprintf(stderr,
"fabs(3.14159265358979312 - *c_double) > 1.0E-13\n");
--ret;
}
if (memcmp(**c_character_string,"PASSED CHARACTER STRING",23)
!= 0)
{
fprintf(stderr,"**c_character_string not %s\n",
"\"PASSED CHARACTER STRING\"");
--ret;
} /***********************************************************
* Fortran program will check for a correct return code. *
***********************************************************/
fprintf(stderr,"CECFFOR ENDED\n");
return(ret);
}