Object: using c++ to call fortran subroutine (f77 file) using command
Platform: windows 10, Visual Studio 2015, c++ (Intel Parrallel Studio XE 2017), fortran (Intel Visual Fortran)
Testing code source: http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html
Fortran code ( fsub.for ):
subroutine fortfunc(ii,ff)
integer ii
real*4 ff
write(6,100) ii, ff
100 format('ii=',i2,' ff=',f6.3)
return
end
C++ code ( test.cpp ):
#include <iostream>
using namespace std;
extern"C" {
void fortfunc_(int *ii, float *ff);
}
main()
{
int ii=5;
float ff=5.5;
fortfunc_(&ii, &ff);
return 0;
}
Step 1:
command:
gfortran -c fsub.for
output: fsub.o
Step 2:
command:
nm fsub.o
output: name of that fortran function or subroutine being called in C++ (in this example, name is fortfunc_), and then check if name in c++ code matches this name.
Step 3:
command:
g++ test.cpp fsub.o -o call.exe -lgfortran
output: call.exe
Step 4:
command:
call.exe
output: result
The commands and results for Step 1 to Step 4 are as shown as figure below:
path of fsub.for and test.cpp for this test is: C:\Personal\code\C
Reference:
for f77 linking http://stackoverflow.com/questions/5663083/linking-fortran-and-c-binaries-using-gcc/41317505#41317505
for f90 linking http://stackoverflow.com/questions/19276874/mixed-c-and-fortran-linking-issue?rq=1