- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am wonderingthat calling a C++ function from a Fortran subroutine is the same way ascalling from a Fortran subroutine which inside a module?
C++ code
extern"C" void FORTRAN_CALL_C (int *i,int*j)
{
*i=1;
*j=5;
printf("This is Called from Fortran! ");
}
Fortran Code 1 (calling from a subroutine)
subroutine F1(INT_ARG, STR_IN, STR_OUT)
CALL FORTRAN_CALL_C(INT_ARG, STR_IN)
end subroutineF1
Fortran code 2 (calling from a module)
MODULE FDLL
containssubroutine F1(INT_ARG, STR_IN, STR_OUT)
CALL FORTRAN_CALL_C(INT_ARG, STR_IN)
end subroutineF1
I got a message error showing
2>FDLL .obj : error LNK2019: unresolved external symbol FORTRAN_CALL_C referenced in function FDLL_mp_F1
Thank you in advance!
Link Copied
5 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I forgot to mention that the Fortran code is inside a DLL which is called from another C++ function.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I don't see anything wrong from what you have posted here. You should make sure that the object file from the C compilation is being linked in with the DLL. There is no difference in how a C routine is called from within a module or not within a module.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Steve, Thanks you. After I added an interface in the top of the module section, the code works. My another problem is how to define dynamic allocated array in the interface.
moduleEXAMPINTERFACESUBROUTINE FORTRAN_CALL_C(INT_array, INT_array2) !DEC$ ATTRIBUTES REFERENCE :: INT_array, INT_arry2INTEGER INT_array, INT_arry2 ===> How to define a dynamic allocated array here???END SUBROUTINEEND INTERFACE
.....
end module.
Since in the Fortran I have an dynamic allocated array that
pass to C++ function.
Inside the fortran code:
integer noofindex
noofindex=10
allocate(INT_array(noofindex),stat=error)if(error.ne.0) thenwrite(911,*) "allocate INT_array error"stopendifINT_array(:) = 0
Thank you very much!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
That depends on what the C code does. If it just receives a pointer to the array and uses [] notation to index it, then use DIMENSION(*) in the Fortran interface. If the C code understands Fortran array descriptors and uses the descriptor fields (unlikely), then use DIMENSION(:).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you very much! My code works now.
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