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

linking c++ and fortran code

isomorphismus
Beginner
528 Views
Hello everybody,
I want to call a c++-function in a fortran program, using Compaq Visual Fortran as a Fortran compiler and Microsoft Visual C++ as a c++ compiler. I have written the following pieces of code:
the fortran main program:
program frotc

implicit none

integer :: x,y,max
write(*,*) "Bitte geben Sie zwei Zahlen ein: "
read(*,*) x,y

call sum(x,y,max)

write(*,*)"Das Maximum ist: ",max


end program frotc

and the to-be-called c++ function:
extern "C"
{void_stdcall max(int*,int*,int*);
}
void max(int*x,int*y,int*max){if(*x<=*y) *max=*y;
else *max=*x;
}

but I have no clue how to link these two programs together. Id be grateful for every piece of advice!

Thanks,
Isomorphismus
0 Kudos
1 Reply
danhoyt
Beginner
528 Views

If you're linking the two together, the process is pretty straighforward (this example is for VS.NET2003 with IVF):

  1. Create a new, empty solution in VS
  2. Create a new C++ Library project
    1. Change your function paramsto use refs instead of pointers.
  3. Create a new Fortran project and make these property changes in ALL configs:
    1. Change Fortran|External Procedures: "C, REFERENCE"
    2. Add Linker|General|Additional Library Directory: "..{C_PROJECT}$(OutDir)" (replacing {C_PROJECT} with the dir from #2, usually the same as the project name)
    3. Add Linker|Input|Additional Dependencies: "{C_PROJECT}.lib" (replacing {C_PROJECT} with theoutput namefrom #2, usually the same as the project name)
    4. Add Debugging|Working Directory: "$(TargetPath).."
  4. Set Project|Project Dependencies so the Fortran project is dependent on the C project.
  5. Set Project|Set Startup Project to be the Fortran project.

I've attached a solution .ZIP with your code. Since I didn't include my user option files, you'll probably need to do step 3.4, but I think the rest will be done already.

BTW, If you want to do the C++ as a DLL, it's just a little more complicated, and it's easier to move all your projects into the root.

0 Kudos
Reply