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

Mix Language Programming

shahman
Beginner
475 Views

Topic Mix language- Calling Fortran subroutine from C++ code
C++ code

#include

extern "C"

{

void multi_(int *,int *,int * );

}

int main()

{

int x=2;

int y=3;

int xy;

multi_(&x,&y,&xy);

printf ("xy= %d",xy);

}

The following Fortran code was compiledand the created "static library *.lib" was copied to thefolderwhere above C++ code was compiled. The fortran static library *.lib was poited to in "additional Dependencies" in the linker configuration

SUBROUTINE multi(x11,y11,xy11)

integer x11,y11,xy11

xy11=x11*Y11

return

end


However, I when compiling C++ code I get the following error
Error 1 error LNK2019: unresolved external symbol _multi_ referenced in function _main CProgAddApp.obj C-For-Interface

I really apprecite your help

shaw
0 Kudos
2 Replies
mecej4
Honored Contributor III
475 Views
The name decoration depends on the compilers used and the target (32-bit or 64-bit) EXE type. For 32-bit Windows, the name in the C++ program (two places) should be MULTI.

When in doubt, use DUMPBIN to display the unsatisfied externals in the .OBJ file that the C++ compiler and Fortran compiler produce. You may also use compiler options to control the case of external names, (and, to some extent, the addition of leading and trailing underscores).
0 Kudos
TimP
Honored Contributor III
475 Views
USE iso_c_binding

subroutine multi(x11,y11,z11) bind(c,name='multi_')
integer(C_INT) x11,y11,z11
....


Then you don't have to remember that Windows mangling is different from linux.
0 Kudos
Reply