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

how to link to and call a c++ function from a dll of cpp+ functions

golush__william
Beginner
1,202 Views
HI,
I am using visual studio fortran , and my fortran progran needs to call a c++ function that looks like

int HGC_TBP( const int NC, double* LV, double* TEMP);

where for example NC is an integer that might be passed by value,
and the other arguments LV and TEMP are arrays passed by reference with the base address of the array

the C++ function HGC_TBP is in a libraryfile C:\\HSI\\MYCPPDLL.lib that I added to the project using tools; options; intel fortran; general; libraries

but when I attempt to build the fortran program I get link error LNK2019 unresolved external symbol HGC_TBP


so firstly I need to clear the LNK2019 error, and secondly I need some help about what the INTERFACE , if any in the fortran program will look like

in the past I have used something along the lines of

INTERFACE
INTEGER FUNCTION HGC_TBP(NC, LV, TEMP)
!DEC$ATTRIBUTES STDCALL, ALIAS : '_HGC_TBP@20':: HGC_TBP
!DEC$ATTRIBUTES REFERENCE :: LV,TEMP
REAL (8) , LV(*),TEMP(*)
END FUNCTION HGC_TBP
END INTERFACE


where the @20 shows the lenght of the argument list is 4+8+8 =20, since NC is integer*4, and the arrays LV, TEMP are each double precision ,

any help much appreciated,
thanks
bill
0 Kudos
4 Replies
Steven_L_Intel1
Employee
1,202 Views
The problem with "looks like" is that it is often different from what the actual code has.

You will need 'extern "C" __declspec(dllexport)' on the C++ side. Your "in the past" code assumes STDCALL, which is also not specified in your "looks like". I would suggest not using STDCALL and changing your Fortran declaration to be the following:

INTERFACE
INTEGER(C_INT) FUNCTION HGC_TBP(NC, LV, TEMP) BIND(C, NAME="HGC_TBP")
USE, INTRINSIC :: ISO_C_BINDING
INTEGER(C_INT), VALUE :: NC
REAL (C_DOUBLE) , LV(*),TEMP(*)
END FUNCTION HGC_TBP
END INTERFACE
0 Kudos
JVanB
Valued Contributor II
1,202 Views
I disagree with your arithmetic.sizeof(int) = 4, but sizeof(double *) = 4 because C is getting passed a pointer, not a double. Thus @12 because 4+4+4=12. Also you could look at MYCPPDLL.lib with DUMPBIN and see what symbols it exports.
0 Kudos
TimP
Honored Contributor III
1,202 Views
In X64 mode, the pointers will occupy 8 bytes, but the idea of using STDCALL is hopeless. You would need a very good reason for avoiding extern "C" and iso_c_binding.
0 Kudos
Steven_L_Intel1
Employee
1,202 Views
On x64, STDCALL and the @n convention don't exist.
0 Kudos
Reply