<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Quote:&amp;lt;a href=&amp;quot;https:/ in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1350035#M159274</link>
    <description>&lt;P&gt;I have not tried the code, but this line:&lt;/P&gt;
&lt;LI-CODE lang="cpp"&gt;for (int t=0 ; t &amp;lt; top ; t++) cout &amp;lt;&amp;lt; s &amp;lt;&amp;lt; " ";&lt;/LI-CODE&gt;
&lt;P&gt;should probably be:&lt;/P&gt;
&lt;LI-CODE lang="cpp"&gt;for (int t=0 ; t &amp;lt; top ; t++) cout &amp;lt;&amp;lt; s[t] &amp;lt;&amp;lt; " ";&lt;/LI-CODE&gt;
&lt;P&gt;Otherwise the address of s is printed - s is an array after all.&lt;/P&gt;</description>
    <pubDate>Mon, 10 Jan 2022 13:20:35 GMT</pubDate>
    <dc:creator>Arjen_Markus</dc:creator>
    <dc:date>2022-01-10T13:20:35Z</dc:date>
    <item>
      <title>Calling C++ (cpp objects) from a Fortran subroutine</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1110556#M129101</link>
      <description>&lt;P&gt;Is there a way to access a C++ object from a Fortran subroutine? My subroutine is like this:&lt;/P&gt;

&lt;P&gt;subroutine fsub(...)&lt;/P&gt;

&lt;P&gt;! If first time calling, call to a cpp function and do initialization (i.e., create the c++ object) .&lt;/P&gt;

&lt;P&gt;! Call to a cpp function which will access to the c++ object and do calculations.&lt;/P&gt;

&lt;P&gt;end&lt;/P&gt;

&lt;P&gt;The subroutine "fsub" will be called by the Fortran "main" program many times. I guess the problem is how to keep the c++ object alive each time after the subroutine "fsub" returns and how to access the c++ object in the next time.&lt;/P&gt;

&lt;P&gt;I have been stuck and frustrated on this problem for weeks. Appreciate any assistance.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;/P&gt;

&lt;P&gt;Jason&lt;/P&gt;</description>
      <pubDate>Thu, 18 May 2017 20:45:49 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1110556#M129101</guid>
      <dc:creator>lu__jason</dc:creator>
      <dc:date>2017-05-18T20:45:49Z</dc:date>
    </item>
    <item>
      <title>Quote:&lt;a href="https:/</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1110557#M129102</link>
      <description>&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;Jason L. wrote:&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;

&lt;P style="word-wrap: break-word; font-size: 12px;"&gt;Is there a way to access a C++ object from a Fortran subroutine? My subroutine is like this:&lt;/P&gt;

&lt;P style="word-wrap: break-word; font-size: 12px;"&gt;subroutine fsub(...)&lt;/P&gt;

&lt;P style="word-wrap: break-word; font-size: 12px;"&gt;! If first time calling, call to a cpp function and do initialization (i.e., create the c++ object) .&lt;/P&gt;

&lt;P style="word-wrap: break-word; font-size: 12px;"&gt;! Call to a cpp function which will access to the c++ object and do calculations.&lt;/P&gt;

&lt;P style="word-wrap: break-word; font-size: 12px;"&gt;end&lt;/P&gt;

&lt;P style="word-wrap: break-word; font-size: 12px;"&gt;The subroutine "fsub" will be called by the Fortran "main" program many times. I guess the problem is how to keep the c++ object alive each time after the subroutine "fsub" returns and how to access the c++ object in the next time.&lt;/P&gt;

&lt;P style="word-wrap: break-word; font-size: 12px;"&gt;I have been stuck and frustrated on this problem for weeks. Appreciate any assistance.&lt;/P&gt;

&lt;P style="word-wrap: break-word; font-size: 12px;"&gt;Thanks,&lt;/P&gt;

&lt;P style="word-wrap: break-word; font-size: 12px;"&gt;Jason&lt;/P&gt;

&lt;P&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;

&lt;P&gt;@Jason,&lt;/P&gt;

&lt;P&gt;Sorry to read you "&lt;SPAN style="font-size: 12px;"&gt;have been stuck and frustrated on this problem for weeks&lt;/SPAN&gt;".&lt;/P&gt;

&lt;P&gt;Please note Fortran does NOT provide direct interoperability with C++ classes and Fortran code can have no direct understanding of object instances created in C++. &amp;nbsp;Rather Fortran provides interoperability with the C companion processor which you will know is similar to what C++ provides with extern "C" entities. &amp;nbsp;Other&amp;nbsp;more knowledgeable Fortran and C++ experts may be able to guide you toward better solutions toward your needs, but if I understood your post correctly, then what I suggest you try&amp;nbsp;is a wrapper layer using appropriate extern "C" functions for&amp;nbsp;&lt;SPAN style="font-size: 13.008px;"&gt;the operations&amp;nbsp;involving your C++ classes &lt;/SPAN&gt;and utilize, as is commonly done in C &amp;lt;-&amp;gt; C++ invocations, an opaque pointer to the C++ object instance in these functions. &amp;nbsp;Do pay close attention to the lifecycle of the C++ instance, holding a reference to the opaque pointer from your Fortran code and to instantiate the class as needed and to delete it when done. &amp;nbsp;Shown below is an example illustrating this scheme using a simple-minded C++ class for an integer stack. &amp;nbsp; Note you should be able to make do without any actual C code.&lt;/P&gt;

&lt;P&gt;Hope this helps, good luck with your work.&lt;/P&gt;

&lt;PRE class="brush:cpp;"&gt;#include &amp;lt;iostream&amp;gt;
using namespace std;

class IntStack
{
public:
   IntStack(int num) {  top = 0; maxelem = num; s = new int[maxelem]; }
   ~IntStack() { if (s != nullptr) delete[] s; }
   void  push(int t)
   {
      if (top == maxelem) return;
      s[top++] = t;
   }
   int pop()
   {
      if (top == 0) return -1;
      return s[--top];
   }
   void display()
   {
     cout &amp;lt;&amp;lt; "IntStack::Display\n";
     if (top == 0) { cout &amp;lt;&amp;lt; "(empty)\n";  return; }
     for (int t=0 ; t &amp;lt; top ; t++) cout &amp;lt;&amp;lt; s&lt;T&gt; &amp;lt;&amp;lt; " ";
     cout &amp;lt;&amp;lt; "\n";
   }
   int   empty()  {  return top == 0;  }
private:
   int *s;
   int top;
   int maxelem;
};

typedef void * OpaqueObject;

// Function prototypes
extern "C" {
   OpaqueObject GetObject(int);
   void ExerciseObject1(OpaqueObject, int);
   void ExerciseObject2(OpaqueObject);
   void DeleteObject(OpaqueObject);
}

OpaqueObject GetObject(int s_size) {

   IntStack *s = new IntStack(s_size);

   return (OpaqueObject)s;

}

void ExerciseObject1(OpaqueObject foo, int i) {

   IntStack *s = (IntStack *)foo;

   s-&amp;gt;push(i);

   return;

}

void DeleteObject(OpaqueObject foo) {

   IntStack *s = (IntStack *)foo;

   delete(s);

   return;
}

void ExerciseObject2(OpaqueObject foo) {

   IntStack *s = (IntStack *)foo;

   s-&amp;gt;display();

   return;
}
&lt;/T&gt;&lt;/PRE&gt;

&lt;PRE class="brush:fortran;"&gt;module m

   use, intrinsic :: iso_c_binding, only : c_int, c_ptr, c_null_ptr, c_associated

   implicit none

   private

   interface

      function GetObject( s_size ) result( optr )bind(C, name="GetObject")

         import :: c_int, c_ptr

         implicit none

         ! Argument list
         integer(c_int), intent(in), value :: s_size
         ! Function result
         type(c_ptr) :: optr

      end function GetObject

      subroutine ExerciseObject1( optr, ival ) bind(C, name="ExerciseObject1")

         import :: c_int, c_ptr

         implicit none

         ! Argument list
         type(c_ptr), intent(in), value :: optr
         integer(c_int), intent(in), value :: ival

      end subroutine ExerciseObject1

      subroutine ExerciseObject2( optr ) bind(C, name="ExerciseObject2")

         import :: c_ptr

         implicit none

         ! Argument list
         type(c_ptr), intent(in), value :: optr

      end subroutine ExerciseObject2

      subroutine DeleteObject( optr ) bind(C, name="DeleteObject")

         import :: c_ptr

         implicit none

         ! Argument list
         type(c_ptr), intent(in), value :: optr

      end subroutine DeleteObject

   end interface

   ! Hold Object
   type(c_ptr), save :: obj = c_null_ptr

   public :: Create
   public :: Employ
   public :: Destroy

contains

   subroutine Create( prob_size )

      ! Argument list
      integer(c_int), intent(in) :: prob_size

      if ( c_associated(obj) ) then
         call DeleteObject( obj )
      end if

      obj = GetObject( s_size=prob_size )

      return

   end subroutine Create

   subroutine Employ( dat )

      ! Argument list
      integer(c_int), intent(in) :: dat

      if ( c_associated(obj) ) then
         call ExerciseObject1( obj, ival=dat )
         call ExerciseObject2( obj )
      end if

      return

   end subroutine Employ

   subroutine Destroy()

      ! Argument list

      if ( c_associated(obj) ) then
         call DeleteObject( obj )
         obj = c_null_ptr
      end if

      return

   end subroutine Destroy

end module m
&lt;/PRE&gt;

&lt;PRE class="brush:fortran;"&gt;program p

   use, intrinsic :: iso_fortran_env, only : compiler_version

   use m, only : Create, Employ, Destroy

   print *, "Compiler Version: ", compiler_version()

   ! Create C++ object
   call Create( 10 )

   ! Use the object a few times
   call Employ( 1 )
   call Employ( 2 )
   call Employ( 3 )

   ! Destroy the object before program termination
   call Destroy()

   stop

end program p
&lt;/PRE&gt;

&lt;P&gt;Upon execution,&lt;/P&gt;

&lt;PRE class="brush:plain;"&gt; Compiler Version:
 Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(

 R) 64, Version 18.0.0.065 Beta Build 20170320

IntStack::Display
1
IntStack::Display
1 2
IntStack::Display
1 2 3
Press any key to continue . . .&lt;/PRE&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 17:56:43 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1110557#M129102</guid>
      <dc:creator>FortranFan</dc:creator>
      <dc:date>2017-05-19T17:56:43Z</dc:date>
    </item>
    <item>
      <title>Thank you very much! It seems</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1110558#M129103</link>
      <description>&lt;P&gt;Thank you very much! It seems to be exactly what I need.&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2017 18:20:26 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1110558#M129103</guid>
      <dc:creator>lu__jason</dc:creator>
      <dc:date>2017-05-19T18:20:26Z</dc:date>
    </item>
    <item>
      <title>Thanks to @FortranFan  for</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1110559#M129104</link>
      <description>&lt;P&gt;Thanks to @FortranFan&amp;nbsp; for kindly sharing this code. Can you kindly show usage of namespace for classes?&lt;/P&gt;

&lt;P&gt;best,&lt;/P&gt;</description>
      <pubDate>Sun, 04 Mar 2018 00:43:33 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1110559#M129104</guid>
      <dc:creator>Bhattacharyya__Abhij</dc:creator>
      <dc:date>2018-03-04T00:43:33Z</dc:date>
    </item>
    <item>
      <title>Hello @FortranFan,</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1110560#M129105</link>
      <description>&lt;P&gt;Hello @FortranFan,&lt;/P&gt;

&lt;P&gt;I have tested your code, and it works. So thank you. I'm interested in the use of the s array in fortran. I tried to modify it to get the s pointer of C++ and use it in fortran but I failed. I don't know how to declare s in fortran to obtain a subroutine GetS(&amp;nbsp; s ) bind(C, name="GetS") who would permit me to get it.&lt;/P&gt;

&lt;P&gt;I wish you could advise me on this problem.&lt;/P&gt;

&lt;P&gt;Thanks.&lt;/P&gt;

&lt;P&gt;Romain&lt;/P&gt;</description>
      <pubDate>Thu, 06 Sep 2018 16:12:02 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1110560#M129105</guid>
      <dc:creator>Fichepoil__Romain</dc:creator>
      <dc:date>2018-09-06T16:12:02Z</dc:date>
    </item>
    <item>
      <title>Re: Quote:&lt;a href="https:/</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1350032#M159271</link>
      <description>&lt;P&gt;Thanks for this example....&lt;/P&gt;
&lt;P&gt;Please note it does not give the same thing with gcc (would you know how to modify the code ?)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Pierre&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt; Compiler Version: GCC version 6.3.0 20170516&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;IntStack::Display&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;0x5611f730cb90&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;IntStack::Display&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;0x5611f730cb90 0x5611f730cb90&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;IntStack::Display&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;0x5611f730cb90 0x5611f730cb90 0x5611f730cb90&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jan 2022 13:10:41 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1350032#M159271</guid>
      <dc:creator>Bastringue</dc:creator>
      <dc:date>2022-01-10T13:10:41Z</dc:date>
    </item>
    <item>
      <title>Re: Quote:&lt;a href="https:/</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1350035#M159274</link>
      <description>&lt;P&gt;I have not tried the code, but this line:&lt;/P&gt;
&lt;LI-CODE lang="cpp"&gt;for (int t=0 ; t &amp;lt; top ; t++) cout &amp;lt;&amp;lt; s &amp;lt;&amp;lt; " ";&lt;/LI-CODE&gt;
&lt;P&gt;should probably be:&lt;/P&gt;
&lt;LI-CODE lang="cpp"&gt;for (int t=0 ; t &amp;lt; top ; t++) cout &amp;lt;&amp;lt; s[t] &amp;lt;&amp;lt; " ";&lt;/LI-CODE&gt;
&lt;P&gt;Otherwise the address of s is printed - s is an array after all.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jan 2022 13:20:35 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1350035#M159274</guid>
      <dc:creator>Arjen_Markus</dc:creator>
      <dc:date>2022-01-10T13:20:35Z</dc:date>
    </item>
    <item>
      <title>Re: Quote:&lt;a href="https:/</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1350059#M159275</link>
      <description>&lt;P&gt;Thanks a lot, it was just this &lt;LI-EMOJI id="lia_slightly-smiling-face" title=":slightly_smiling_face:"&gt;&lt;/LI-EMOJI&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jan 2022 14:22:11 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Calling-C-cpp-objects-from-a-Fortran-subroutine/m-p/1350059#M159275</guid>
      <dc:creator>Bastringue</dc:creator>
      <dc:date>2022-01-10T14:22:11Z</dc:date>
    </item>
  </channel>
</rss>

