Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28712 Discussions

Passing fortran variables to C in mixed programming

Wee_Beng_T_
Beginner
228 Views

Hi,

I've a subroutine that's written in C. My main program and everything else are in fortran 90. I have a global data module which consist of some variables and it can be assessed globally ie

module global_data

implicit none

save

real :: dd(10)

end module global_data

I would like to pass the values of a variable (e.g. dd) to a C subroutine, do some calculation, and out comes the result of the variable.

How can I do this? Is there a sample code somewhere? I tried to find some info on the net but they seem quite complicated...

Btw, my environment is linux and it's using ifort and icc ver 9.1

Thanks in advance!

0 Kudos
2 Replies
Steven_L_Intel1
Employee
228 Views
Just call the C routine and pass the variable as an argument. On the C side, be sure to declare the argument as *float or something similar. There is a chapter of the documentation on mixed-language programming which will be of interest. You'll want the C routine to have a lowercase name with a trailing underscore, as that is the convention used for Fortran routines on Linux.
0 Kudos
TimP
Honored Contributor III
228 Views
Since you're learning, you should consider the advantage of starting out with a Fortran compiler which supports the current standard method, ISO C interoperability. For example, you could install a pre-built Fortran from the gfortran wiki, or use ifort 10.1. As you point out, it could be confusing if you don't choose between current and legacy methods, which differ somewhat between linux, Windows, and various flavors of Unix.
If you mean that you want to use ifort 9.1, you should find examples in the documentation in the /doc/ directory of the installation. Both legacy linux style linkage and DEC directive methods are supported.
0 Kudos
Reply