Calling FORTRAN From C++.

I'm trying to call FORTRAN From C++.

I have the follwoign but its not working.Any tips?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
MODULE ARRAY_STORAGE 
      
      REAL*8, ALLOCATABLE :: ARRAY(:) 
      
      END MODULE ARRAY_STORAGE 

      
      function ffunc(n1,n2) 
      
      USE ARRAY_STORAGE 

      integer ffunc 
      character*256 filename 
      integer n1,n2,iret 
      integer*4 stat 
      
      ALLOCATE (ARRAY(10),stat=stat) 
      if (stat/=0) then 
         write (*,*) 'problem allocating array : stat=',stat 
      else 
         write (*,*) 'array allocated as size', size(array)*8./(2.**20), 
     +' mb at LOC',loc(array) 
      end if 

      
      ffunc = n1+n2 
      
      return 
      
      end


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream> 
extern "C" 
{ 
   //int __stdcall  ffunc(int*, int*); 
   float ARRAY_STORAGE_mp_ARRAY(); 
   //int ffunc(int*, int*); 
    
    
} 

extern "C" 
{ 
   //int __stdcall  ffunc(int*, int*); 
   //float ARRAY_STORAGE_mp_ARRAY(); 
   int ffunc(int*, int*); 
    
    
} 

int main() 
{ 
    int n1 = 1; 
    int n2 = 2; 
    
   std::cout << "Calling from C++ to Fortran, arguments: " << n1 << ", " << n2 << '\n'; 
    
   int r = ffunc(&n1,&n2); 
    
   //std::cout << "THe return value from fortran was " << r << '\n'; 


    

}
Define "not working".
Also, this won't link as written, every occurrence of ffunc needs to be ffunc_, in the C++ file.
With that change, it compiles and runs for me:
1
2
3
$ ./test 
Calling from C++ to Fortran, arguments: 1, 2
 array allocated as size 0.7629394531E-04  mb at LOC 805435768
Thanks Cubbi

I'm using Visual Studio Environment. I don't need ffunc_.

I can confirm because if I comment USE ARRAY_STORAGE and comment the module portionas well in .F file- all works and the function is called.

The problem happens when the USE ARRAY_STORAGE is put in

One of the error is: Error 25 fatal error LNK1169: one or more multiply defined symbols found D:\ajay_2012\FFES\fortran_to_cpp\cpptest\Debug\cpptest.exe cpptest


And other erros are:

Error 1 error LNK2005: __CrtSetCheckCount already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 2 error LNK2005: _exit already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 3 error LNK2005: __exit already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 4 error LNK2005: __cexit already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 5 error LNK2005: __amsg_exit already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 6 error LNK2005: __initterm_e already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 7 error LNK2005: __crt_debugger_hook already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 8 error LNK2005: __invoke_watson already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 9 error LNK2005: __configthreadlocale already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 10 error LNK2005: __encode_pointer already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 11 error LNK2005: __decode_pointer already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 12 error LNK2005: __XcptFilter already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 13 error LNK2005: ___xi_a already defined in MSVCRTD.lib(cinitexe.obj) LIBCMTD.lib cpptest
Error 14 error LNK2005: ___xi_z already defined in MSVCRTD.lib(cinitexe.obj) LIBCMTD.lib cpptest
Error 15 error LNK2005: ___xc_a already defined in MSVCRTD.lib(cinitexe.obj) LIBCMTD.lib cpptest
Error 16 error LNK2005: ___xc_z already defined in MSVCRTD.lib(cinitexe.obj) LIBCMTD.lib cpptest
Error 17 error LNK2005: "void __cdecl terminate(void)" (?terminate@@YAXXZ) already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 18 error LNK2005: __lock already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 19 error LNK2005: __unlock already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 20 error LNK2005: _mainCRTStartup already defined in MSVCRTD.lib(crtexe.obj) LIBCMTD.lib cpptest
Error 21 error LNK2005: ___set_app_type already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 22 error LNK2005: __CrtDbgReportW already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Warning 23 warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library cpptest cpptest
Warning 24 warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library cpptest cpptest
Thanks Cubbi.
I'm using Visual Studo and it works as ffunc

Now, I get the errros as:
Error 1 error LNK2005: __CrtSetCheckCount already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 2 error LNK2005: _exit already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 3 error LNK2005: __exit already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 4 error LNK2005: __cexit already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 5 error LNK2005: __amsg_exit already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 6 error LNK2005: __initterm_e already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 7 error LNK2005: __crt_debugger_hook already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 8 error LNK2005: __invoke_watson already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 9 error LNK2005: __configthreadlocale already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 10 error LNK2005: __encode_pointer already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 11 error LNK2005: __decode_pointer already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 12 error LNK2005: __XcptFilter already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 13 error LNK2005: ___xi_a already defined in MSVCRTD.lib(cinitexe.obj) LIBCMTD.lib cpptest
Error 14 error LNK2005: ___xi_z already defined in MSVCRTD.lib(cinitexe.obj) LIBCMTD.lib cpptest
Error 15 error LNK2005: ___xc_a already defined in MSVCRTD.lib(cinitexe.obj) LIBCMTD.lib cpptest
Error 16 error LNK2005: ___xc_z already defined in MSVCRTD.lib(cinitexe.obj) LIBCMTD.lib cpptest
Error 17 error LNK2005: "void __cdecl terminate(void)" (?terminate@@YAXXZ) already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 18 error LNK2005: __lock already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 19 error LNK2005: __unlock already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 20 error LNK2005: _mainCRTStartup already defined in MSVCRTD.lib(crtexe.obj) LIBCMTD.lib cpptest
Error 21 error LNK2005: ___set_app_type already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest
Error 22 error LNK2005: __CrtDbgReportW already defined in MSVCRTD.lib(MSVCR90D.dll) LIBCMTD.lib cpptest

Can you help?
Sporry, not familiar with Visual Studio (I use IBM, Sun, and, to lesser degree, GNU toolchains)
Topic archived. No new replies allowed.