Silverfrost Forums

Welcome to our forums

Fortran and C

16 Jul 2008 4:04 #3465

Your reply stated that:

'To provide a link between Fortran and C you will need to use C_EXTERNAL statements in the Fortran and export your C routines in a specific way. Its not so tricky and the manual has info on this at:

Win32 Platform->Mixed Language Programming->Calling C/C++ from FTN95

Basically you need to add extern 'C' to your C routine definitions and create corresponding C_EXTERNAL definitions in your Fortran. The C routines will need exporting from the DLL (depends on the C compiler you are using exactly how this is done). You can then link directly with the DLL using SLINK (no need for lib files). '

I'm using the Salford C compiler, scc, at this stage

Would you please be able to take a couple of minutes to make the necessary modifications to the following example code, as this will save me (and probably you) an awful lot of time.

program test_wrapper

dimension xxx(10), yyy(10)

npt = 10

do ip = 1, npt

xxx(ip) = ip

yyy(ip) = 1.

end do

xv1 = 20. yv1 = 30.

itt = 6

call wrap_c_to_Fortran ( itt, npt, xxx, yyy, xpr, 1 xv1, yv1, pv1 )

stop end

c I would like to be able to replace this subroutine c by C code (see my first try below)

c and make whatever changes are necessary to the remaining Fortran c and modified c code to have the example compile and run

subroutine wrap_c_to_Fortran ( itt, npt, xxx, yyy, xpr, 1 xv1, yv1, pv1 )

dimension xxx(), yyy()

c write data

write (itt,'(/ '' XXX '')' )

write (itt,'( 10g12.4 )') (xxx(ip),ip=1,npt)

do ip = 1, npt

write (itt,'(/ '' XXX(i) '', g12.4 )') xxx(ip)

end do

write (itt,'(/ '' YYY '')' )

write (itt,'( 10g12.4 )') (yyy(ip),ip=1,npt)

do ip = 1, npt

write (itt,'(/ '' YYY(i) '', g12.4 )') yyy(ip)

end do

write (itt,'(/ '' XV1 '', g12.4 )') xv1

write (itt,'(/ '' YV1 '', g12.4 )') yv1

call calc_prod ( npt, xxx, yyy, xpr, xv1, yv1, pv1 )

write (itt,'(/ '' XXX * YYY = '', g12.4 )') xpr

write (itt,'(/ '' XV1 * YV1 = '', g12.4 )') pv1

return end

subroutine calc_prod ( npt, xxx, yyy, xpr, xv1, yv1, pv1 )

dimension xxx(), yyy()

xpr = 0.

do ip = 1, npt

xpr = xpr + xxx(ip) * yyy(ip)

end do

pv1 = xv1 * yv1

return end

#include <stdio.h>

extern int calc_prod ( long, long, double *, double *, double, double, double, double );

int wrap_c_to_Fortran ( long itt, long npt, // number of observations double *xxx, double *yyy, double xpr, double xv1, double yv1, double pv1 )

// xxx(ip) = ip yyy(ip) = 1. xpr = xpr + xxx(ip) * yyy(ip); ip = 1, npt

// xv1 = 20. yv1 = 30. pv1 = xv1 * yv1

{ short ip;

long npv1;

for(ip=1;ip<=npt;ip++) printf( '%12.4lf ', xxx[ip] );

for(ip=1;ip<=npt;ip++) printf( '%12.4lf ', yyy[ip] );

printf( '%12.4lf ', xv1 );

printf( '%12.4lf ', yv1 );

calc_prod ( npt, xxx, yyy, xpr, xv1, yv1, pv1 );

printf( '%12.4lf ', xpr );

printf( '%12.4lf ', pv1 );

// sprintf(szBuff, '\n\nC V means - C V %3ld vs C V %ld', ncvY, ncvX); // write (itt,'(/ '' XXX * YYY = '', g12.4 )') xpr // write (itt,'(/ '' XV1 * YV1 = '', g12.4 )') pv1

npv1 = pv1;

return npv1;

}

Please login to reply.