replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - Mixing Fortran and C++
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Mixing Fortran and C++

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
eric_carwardine



Joined: 13 Jun 2009
Posts: 70
Location: Perth, Western Australia

PostPosted: Sun Mar 07, 2010 11:17 am    Post subject: Mixing Fortran and C++ Reply with quote

G'day, folks Very Happy

Are there examples of Mixed Language programming here? Specifically, I wish to call C++ functions from a Fortran mainline. I've created the following two files, both of which compile without errors under their respective compilers:

Fortran main, test.f:

Code:

C program calls C++ 'multiply' which forms the product of two integers,
C m and n, and returns result in k.
      character ch*1
C
      m=-1
      n=99
      call multiply(m, n, k)
C
      write(*,11) k
11    format(' k = ',i6)
      read(*,21) ch
21    format(a1)
      stop
      end


C++ function, multiply.c:

Code:

extern "C"
      {
   void __stdcall multiply(int *,int *,int *);
      }
      void __stdcall multiply(int *m, int *n, int *k)
      {
   // Compute the product of m and n, return in k
   int k1;
   int k2;
   k1=*m;
   k2=*n;
   *k=k1*k2;
   return;
      }


Then the following sequence of commands:

Code:

FTN95> ftn95 test.f
FTN95> scc multiply.c
FTN95> slink test.obj multiply.obj
WARNING: Missing symbol MULTIPLY.


What am I doing wrong - or not doing?

Eric
Back to top
View user's profile Send private message Send e-mail Visit poster's website
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8210
Location: Salford, UK

PostPosted: Mon Mar 08, 2010 10:00 am    Post subject: Reply with quote

You need an interface for multiply in your Fortran code.
You can find examples in the standard *.ins files supplied.
Details are in FTN95.chm under Mixed Language programming.
Back to top
View user's profile Send private message AIM Address
eric_carwardine



Joined: 13 Jun 2009
Posts: 70
Location: Perth, Western Australia

PostPosted: Mon Mar 08, 2010 1:54 pm    Post subject: Re: Reply with quote

PaulLaidler wrote:
You need an interface for multiply in your Fortran code.
You can find examples in the standard *.ins files supplied.
Details are in FTN95.chm under Mixed Language programming.


Thanks, Paul;

I found 29 .ins files in the /include folder, but the filenames gave no indication of any Mixed Language Programming content. I double-clicked on a .ins file selected at random, but got a warning that my Internet settings were about to change! I hastily backed-out of that one Smile

I explored FTN95.CHM and found a section titled Mixed Language Programming, but it appears to be empty. I did a partial screen-capture, which I've uploaded to my website, at http://www.kurrattan.net/exhibits/FTN95_mixed.jpg

Eric
Back to top
View user's profile Send private message Send e-mail Visit poster's website
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8210
Location: Salford, UK

PostPosted: Mon Mar 08, 2010 2:59 pm    Post subject: Reply with quote

Try opening win32api.ins in Plato.

You will need something like

STDCALL MULTIPY 'Multiply'(REF,REF,REF)

in your Fortran declarations.

It would be simpler to avoid STDCALL in your C code and to use C_EXTERNAL in your Fortran.

Information in the help file is at

Win32 platform->Mixed language programming->Calling FTN95 from C/C++->Calling C/C++ from FTN95
Back to top
View user's profile Send private message AIM Address
eric_carwardine



Joined: 13 Jun 2009
Posts: 70
Location: Perth, Western Australia

PostPosted: Mon Mar 08, 2010 3:56 pm    Post subject: Re: Reply with quote

PaulLaidler wrote:

Information in the help file is at

Win32 platform->Mixed language programming->Calling FTN95 from C/C++->Calling C/C++ from FTN95


Getting closer Smile

How do I access/invoke the Win32 platform?

Eric
Back to top
View user's profile Send private message Send e-mail Visit poster's website
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8210
Location: Salford, UK

PostPosted: Mon Mar 08, 2010 7:22 pm    Post subject: Reply with quote

The Win32 platform is what you get by default.
The alternative is the .NET platform and you get this by using /CLR on the FTN95 command line.
Back to top
View user's profile Send private message AIM Address
eric_carwardine



Joined: 13 Jun 2009
Posts: 70
Location: Perth, Western Australia

PostPosted: Tue Mar 09, 2010 12:41 pm    Post subject: Re: Reply with quote

PaulLaidler wrote:
The Win32 platform is what you get by default.


G'day, Paul Smile

I'm still not getting it (explanation of Mixed Language Programming). Displayed is:

Quote:

Setting Environment for FTN95 for Microsoft .NET and Win32

c:\Program Files\Silverfrost\FTN95>ftn95 /help


Invoking "help" lead me to the same empty page as previously. See http://www.kurrattan.net/exhibits/FTN95.jpg So next I invoked Plato and went in search of "Mixed Language Programming", but was lead to the same empty page as before.

Does it make a difference that I'm working with files derived from downloading and installing ftn95_personal.exe ?

Eric
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Sebastian



Joined: 20 Feb 2008
Posts: 177

PostPosted: Tue Mar 09, 2010 12:51 pm    Post subject: Reply with quote

Open the help file as usual (as in your screenshot), then click on Contents (upper left) and check the entries for "Win32 platform".
Back to top
View user's profile Send private message
eric_carwardine



Joined: 13 Jun 2009
Posts: 70
Location: Perth, Western Australia

PostPosted: Tue Mar 09, 2010 2:49 pm    Post subject: Re: Reply with quote

Sebastian wrote:
Open the help file as usual (as in your screenshot), then click on Contents (upper left) and check the entries for "Win32 platform".


Thankyou excruciatingly, Sebastian! That certainly did the trick Smile I had everything crossed, waiting for the error messages, but none appeared! Beautiful.

For the benefit of any who may follow in our keystrokes, here is the final state-of-play (any fans of Michael Nyman here?):

Fortran file, test.f:

Code:

C program calls C++ 'Multiply' which forms the product of two integers,
C m and n, and returns result in k.
C
      C_EXTERNAL MULTIPLY 'Multiply' (REF,REF,REF):integer*4
C
      character ch*1
C
      m=-2
      n=4008
      call MULTIPLY(m, n, k)
C
      write(*,11) k
11    format(' k = ',i6)
      read(*,21) ch
21    format(a1)
      stop
      end


C++ file, multiply.c:

Code:

extern "C" void  Multiply(int *m, int *n, int *k)
      {
   // Compute the product of m and n, return in k
   int k1;
   int k2;
   k1=*m;
   k2=*n;
   *k=k1*k2;
   return;
      }
 


Then the following sequence of commands:
Note: ... represents a path spec. Screen output not shown.

Quote:

... \FTN95>ftn95 test.f
... \FTN95>scc multiply.c
... \FTN95>slink test.obj multiply.obj


All of which produced the executable file test.exe, which performed as desired.

Thankyou to all who have made this possible Smile

Eric
Back to top
View user's profile Send private message Send e-mail Visit poster's website
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 8210
Location: Salford, UK

PostPosted: Tue Mar 09, 2010 3:58 pm    Post subject: Reply with quote

Multiply returns a void rather than an INTEGER*4, i.e. it is a subroutine not a function. As you have found, this does not prevent the code from working.

Code:
C_EXTERNAL MULTIPLY 'Multiply' (REF,REF,REF)
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group