Silverfrost Forums

Welcome to our forums

Mixing Fortran and C++

7 Mar 2010 10:17 #6081

**G'day, folks **:D

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:

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:

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:

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

8 Mar 2010 9:00 #6085

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.

8 Mar 2010 12:54 #6087

Quoted from PaulLaidler 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

8 Mar 2010 1:59 #6088

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

8 Mar 2010 2:56 #6089

Quoted from PaulLaidler

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

8 Mar 2010 6:22 #6092

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.

9 Mar 2010 11:41 #6099

Quoted from PaulLaidler 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:

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

9 Mar 2010 11:51 #6100

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

9 Mar 2010 1:49 #6103

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

[size=18:65b5cb69ed]Thankyou excruciatingly, Sebastian![/size:65b5cb69ed] 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:

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:

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.

Thankyou excruciatingly, Sebastian![/size:65b5cb69ed] 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:

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:

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:65b5cb69ed] ... \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

9 Mar 2010 2:58 #6106

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.

C_EXTERNAL MULTIPLY 'Multiply' (REF,REF,REF)
Please login to reply.