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

Calling C++ from Fortran

Stephen_Painchaud
491 Views

I have reada few related posts and have not found the answer I need.

I am using VS2005 and IVF10. I found an example in directory C:Program FilesIntelCompilerFortran10.0.025samplesMixedLanguageFortran_Calls_C. This example passes integers and strings from Fortran to C. I need to pass real arrays. I tried to modify this example without success. The numbers inside the C++ routine where incorrect.

In the spirit of theexample above, I provide my own test case. The C++ code is:

#include

"stdafx.h"

#include

using

namespace std;

extern

"C" void argtest_cpp(int ndata, float *mydata){

cout <<

"ndata = " << ndata << endl;

for (int i = 0; i < ndata; i++)

cout << i <<

" " << *(mydata+i) << endl;

}

The Fortran code is:

PROGRAM

argtest

IMPLICIT NONE

INTERFACE

SUBROUTINE argtest_cpp (ndata, mydata)

!DEC$ ATTRIBUTES C :: argtest_cpp

!DEC$ ATTRIBUTES REFERENCE :: mydata

INTEGER, INTENT(IN) :: ndata

REAL, INTENT(IN) :: mydata(:)

END SUBROUTINE argtest_cpp

END INTERFACE

REAL

mydata(10)

INTEGER

i, ndata

ndata = 10

DO

i = 1, ndata

mydata(i) =

log10(float(i+1))

print *, i, mydata(i)

ENDDO

CALL

argtest_cpp(ndata, mydata)

STOP

END

When I run this code I see that thereal numbers inside the C++ routine are incorrect, but the integer passes properly. I appreciate any help.&n bsp;

0 Kudos
2 Replies
Steven_L_Intel1
Employee
491 Views
You want this instead:

REAL, INTENT(IN) :: mydata(*)


0 Kudos
Stephen_Painchaud
491 Views
It works great. Thank you very much.
0 Kudos
Reply