 |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
eric_carwardine
Joined: 13 Jun 2009 Posts: 70 Location: Perth, Western Australia
|
Posted: Sun Mar 07, 2010 11:17 am Post subject: Mixing Fortran and C++ |
|
|
G'day, folks
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 |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8210 Location: Salford, UK
|
Posted: Mon Mar 08, 2010 10:00 am Post subject: |
|
|
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 |
|
 |
eric_carwardine
Joined: 13 Jun 2009 Posts: 70 Location: Perth, Western Australia
|
Posted: Mon Mar 08, 2010 1:54 pm Post subject: Re: |
|
|
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
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 |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8210 Location: Salford, UK
|
Posted: Mon Mar 08, 2010 2:59 pm Post subject: |
|
|
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 |
|
 |
eric_carwardine
Joined: 13 Jun 2009 Posts: 70 Location: Perth, Western Australia
|
Posted: Mon Mar 08, 2010 3:56 pm Post subject: Re: |
|
|
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
How do I access/invoke the Win32 platform?
Eric |
|
Back to top |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8210 Location: Salford, UK
|
Posted: Mon Mar 08, 2010 7:22 pm Post subject: |
|
|
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 |
|
 |
eric_carwardine
Joined: 13 Jun 2009 Posts: 70 Location: Perth, Western Australia
|
Posted: Tue Mar 09, 2010 12:41 pm Post subject: Re: |
|
|
PaulLaidler wrote: | The Win32 platform is what you get by default.
|
G'day, Paul
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 |
|
 |
Sebastian
Joined: 20 Feb 2008 Posts: 177
|
Posted: Tue Mar 09, 2010 12:51 pm Post subject: |
|
|
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 |
|
 |
eric_carwardine
Joined: 13 Jun 2009 Posts: 70 Location: Perth, Western Australia
|
Posted: Tue Mar 09, 2010 2:49 pm Post subject: Re: |
|
|
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 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
Eric |
|
Back to top |
|
 |
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 8210 Location: Salford, UK
|
Posted: Tue Mar 09, 2010 3:58 pm Post subject: |
|
|
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 |
|
 |
|
|
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
|