forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Fortran and C

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
Norm.Campbell



Joined: 31 Aug 2007
Posts: 66

PostPosted: Wed Jul 16, 2008 5:04 am    Post subject: Fortran and C Reply with quote

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;

}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group