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

Problem calling C++ from Fortran

ferrad02
Beginner
835 Views
I have built a library (under VC2005) containing a fucntion I wish to call from Fortran:

extern "C" {
int cdecl Run_Great() {
... snipped ...
}
}

I include the lib in my Fortran project (Intel 10.1) and am trying to call it with this code:

program RunGreat
implicit none

interface
integer function Run_Great()
!dec$ attributes C ,alias : '_Run_Great' :: Run_Great
end function Run_Great
end interface

integer i
i = Run_Great()

end program RunGreat

However I get an exception when it tries to go into the C++ function. Any idea what I'm doing wrong?

ferrad
0 Kudos
3 Replies
Jugoslav_Dujic
Valued Contributor II
835 Views
Quoting - ferrad02
I have built a library (under VC2005) containing a fucntion I wish to call from Fortran:

...

However I get an exception when it tries to go into the C++ function. Any idea what I'm doing wrong?


When it just tries, or does it actually enter the C function? What happens in the debugger?

I don't see anything wrong in the declarations you posted, but they tell just a small part of the story.
0 Kudos
ferrad02
Beginner
835 Views
Quoting - Jugoslav Dujic

When it just tries, or does it actually enter the C function? What happens in the debugger?

I don't see anything wrong in the declarations you posted, but they tell just a small part of the story.

If I put a break on the Fortran call "i=Run_Great()", It stops there just fine. When I "Step Into", it waits for a second or so, then pops up a window which says:

Unhandled exception at 0x00a6d628 (msvcr80d.dll) in console1.exe:
0xC0000005: Access violation reading location 0x00000031.

It brings up the code for memcpy.asm, line 314:

UnwindUp3:
mov eax,[esi+ecx*4-12] ;U(entry)/V(not) - get dword from source
;V(entry) - spare

The call stack is:

> msvcr80d.dll!memcpy(unsigned char * dst=0x003d8790, unsigned char * src=0x00000031, unsigned long count=15) Line 314 Asm
msvcr80d.dll!memcpy_s(void * dst=0x003d8790, unsigned int sizeInBytes=15, const void * src=0x00000031, unsigned int count=15) Line 67 + 0x11 bytes C
msvcp80d.dll!std::char_traits::_Copy_s(char * _First1=0x003d8790, unsigned int _Size_in_bytes=15, const char * _First2=0x00000031, unsigned int _Count=15) Line 575 + 0x16 bytes C++
msvcp80d.dll!std::_Traits_helper::copy_s<:CHAR_TRAITS> >(char * _First1=0x003d8790, unsigned int _Size=15, const char * _First2=0x00000031, unsigned int _Count=15, std::_Secure_char_traits_tag __formal={...}) Line 707 + 0x15 bytes C++
msvcp80d.dll!std::_Traits_helper::copy_s<:CHAR_TRAITS> >(char * _First1=0x003d8790, unsigned int _Size=15, const char * _First2=0x00000031, unsigned int _Count=15) Line 699 + 0x21 bytes C++
msvcp80d.dll!std::basic_string,std::allocator >::assign(const std::basic_string,std::allocator > & _Right=, unsigned int _Roff=0, unsigned int _Count=4294967295) Line 1059 + 0x25 bytes C++
msvcp80d.dll!std::basic_string,std::allocator >::basic_string,std::allocator >(const std::basic_string,std::allocator > & _Right=) Line 733 C++
libgreatd.dll!101a5833()
[Frames below may be incorrect and/or missing, no symbols loaded for libgreatd.dll]
libgreatd.dll!10195331()
libgreatd.dll!103926d2()
console1.exe!_Run_Great() + 0x20f bytes C++
ntdll.dll!7c92770a()
ntdll.dll!7c94b1b0()
console1.exe!__set_flsgetvalue() Line 288 + 0xc bytes C
console1.exe!write_string(char * string=0x00000000, int len=2089914145, _iobuf * f=0x00000000, int * pnumwritten=0x00000000) Line 2570 + 0xd bytes C++
ntdll.dll!7c91930f()
kernel32.dll!7c80ba4d()
ntdll.dll!7c91930f()
ntdll.dll!7c918f21()
ntdll.dll!7c9101db()
console1.exe!_VEC_memzero(void * dst=0x50000061, int val=2600, int len=1244984) + 0x36 bytes C
ntdll.dll!7c94bc4c()
console1.exe!___intel_new_proc_init.H() + 0x20 bytes
console1.exe!_for__reentrancy_init() + 0x1e bytes
console1.exe!RUNGREAT() Line 15 + 0x5 bytes Fortran


LISTDLLS on console1.exe gives:

D:GREATACFtestConsole1Debug>listdlls console1.exe

ListDLLs V2.23 - DLL lister for Win9x/NT
Copyright (C) 1997-2000 Mark Russinovich
http://www.sysinternals.com

------------------------------------------------------------------------------
console1.exe pid: 2408
Command line: "d:GREATACFtestConsole1debugconsole1.exe"

Base Size Version Path
0x00400000 0xa6000 d:GREATACFtestConsole1debugconsole1.exe
0x7c900000 0xb2000 5.01.2600.5755 C:WINDOWSsystem32ntdll.dll
0x7c800000 0xf6000 5.01.2600.5512 C:WINDOWSsystem32kernel32.dll
0x10000000 0x4a3000 2.01.0002.0000 d:GREATACFtestConsole1debuglibgreatd.dll
0x004b0000 0x325000 d:GREATACFtestConsole1debugSlb.Ecl.Core.PVT.Toolboxd.dll
0x6eed0000 0x7e000 6.06.0893.0101 C:WINDOWSsystem32DFORMD.DLL
0x77c10000 0x58000 7.00.2600.5512 C:WINDOWSsystem32MSVCRT.dll
0x00350000 0x42000 d:GREATACFtestConsole1debugSlb.Ecl.Core.GradientOptimizerd.dll
0x007e0000 0xbc000 7.10.3077.0000 C:WINDOWSsystem32MSVCP71D.dll
0x008a0000 0x87000 7.10.3077.0000 C:WINDOWSsystem32MSVCR71D.dll
0x00930000 0xfe000 8.00.50727.0762 C:WINDOWSWinSxSx86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.762_x-ww_5490cd9fMSVCP80D.dll
0x00a30000 0x121000 8.00.50727.0762 C:WINDOWSWinSxSx86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.762_x-ww_5490cd9fMSVCR80D.dll
0x76c90000 0x28000 5.01.2600.5512 C:WINDOWSsystem32imagehlp.dll
0x003f0000 0xf000 10.01.0105.0105 C:Program FilesIntelCompilerFortran10.1.021IA32Bin1033ifcore_msg.dll
0x00c60000 0x5000 10.01.0105.0105 C:Program FilesIntelCompilerFortran10.1.021IA32Bin1033irc_msg.dll
0x59a60000 0xa1000 5.01.2600.5512 C:WINDOWSsystem32DBGHELP.dll
0x77c00000 0x8000 5.01.2600.5512 C:WINDOWSsystem32VERSION.dll
0x77dd0000 0x9b000 5.01.2600.5755 C:WINDOWSsystem32ADVAPI32.dll
0x77e70000 0x92000 5.01.2600.5512 C:WINDOWSsystem32RPCRT4.dll
0x77fe0000 0x11000 5.01.2600.5512 C:WINDOWSsystem32Secur32.dll

Let me know if there is anything else I can provide to help diagnose the issue.
0 Kudos
ferrad02
Beginner
835 Views
The problem is in the code I snipped: when this is removed, it works fine. The snipped code contains a call to a DLL which obviously is the cause, I will pursue that. Thanks.
0 Kudos
Reply