Pages

Sunday, April 7, 2013

Calling Fortran from C++

Compilers: gfortran, g++

increment.f90:
subroutine increment(n)
integer n
n=n+1
return
end subroutine

callf.cpp:
#include<iostream>

extern "C" { int increment_( int& ); }

int main(int argc, char **argv) {
  int i=0;
  int j=0;
  for(j=0;j<10;j++)
  {
 increment_(i);
 std::cout << i << "\n";
  }
  return 1;
}

Commands:
$ gfortran -c increment.f90 
$ g++ -o called callf.cpp increment.o -lgfortran

Output:
$ ./called 
1
2
3
4
5
6
7
8
9
10


No comments:

Post a Comment