- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
On x64, STDCALL and the @n convention don't exist.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page