linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Calling fortran from C
@ 2003-04-12  8:46 Elias Athanasopoulos
  2003-04-12 10:15 ` Glynn Clements
  0 siblings, 1 reply; 5+ messages in thread
From: Elias Athanasopoulos @ 2003-04-12  8:46 UTC (permalink / raw)
  To: linux-c-programming

Hello!

Does anyone know if I can call fortran code from C using the GCC toolchain?

Elias

-- 
University of Athens			I bet the human brain 
Physics Department				is a kludge --Marvin Minsky 

	

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Calling fortran from C
  2003-04-12  8:46 Calling fortran from C Elias Athanasopoulos
@ 2003-04-12 10:15 ` Glynn Clements
  2003-04-16 16:53   ` Elias Athanasopoulos
  0 siblings, 1 reply; 5+ messages in thread
From: Glynn Clements @ 2003-04-12 10:15 UTC (permalink / raw)
  To: Elias Athanasopoulos; +Cc: linux-c-programming


Elias Athanasopoulos wrote:

> Does anyone know if I can call fortran code from C using the GCC toolchain?

Yes.

Include g2c.h to get definitions for the fortran types, and link with
-lg2c to get the functions which fortran code requires.

You also need to write the prototypes for any fortran functions which
you wish to call from C.

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Calling fortran from C
  2003-04-12 10:15 ` Glynn Clements
@ 2003-04-16 16:53   ` Elias Athanasopoulos
  2003-04-16 17:42     ` Glynn Clements
  0 siblings, 1 reply; 5+ messages in thread
From: Elias Athanasopoulos @ 2003-04-16 16:53 UTC (permalink / raw)
  To: Glynn Clements; +Cc: linux-c-programming

Hi Glynn,

On Sat, Apr 12, 2003 at 11:15:15AM +0100, Glynn Clements wrote:
> > Does anyone know if I can call fortran code from C using the GCC toolchain?
> 
> Yes.
> 
> Include g2c.h to get definitions for the fortran types, and link with
> -lg2c to get the functions which fortran code requires.

Thank you for the reply and extremely sorry for my late response.

I have done all these, except from...

> You also need to write the prototypes for any fortran functions which
> you wish to call from C.

...this.

My problem is that my app crashes whenever it calls a simple fortran 
function.

I've tested the whole stuff with:

	PROGRAM FOO 

        END 

	FUNCTION DUMMY (I)
	INTEGER I
	DUMMY = I               
	END

which I compiled: % g77 bar.f -c -o bar.o
					
and with:

int main (void)
{
	printf ("%d\n", dummy_ (10);
	return 1;
}

which I compiled: %gcc foo.c bar.o -lg2c -L/path/to/g2c -o main

(libg2c is an archive located in: /usr/lib/gcc-lib/i386-redhat-linux/2.96)

Now, 'main' crashes in dummy_ () and gdb doesn't help, since it produces
no further stack output. 

Regards,
Elias

-- 
University of Athens			I bet the human brain 
Physics Department				is a kludge --Marvin Minsky 

	

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Calling fortran from C
  2003-04-16 16:53   ` Elias Athanasopoulos
@ 2003-04-16 17:42     ` Glynn Clements
  2003-04-17 15:23       ` Elias Athanasopoulos
  0 siblings, 1 reply; 5+ messages in thread
From: Glynn Clements @ 2003-04-16 17:42 UTC (permalink / raw)
  To: Elias Athanasopoulos; +Cc: linux-c-programming


Elias Athanasopoulos wrote:

> > > Does anyone know if I can call fortran code from C using the GCC toolchain?
> > 
> > Yes.
> > 
> > Include g2c.h to get definitions for the fortran types, and link with
> > -lg2c to get the functions which fortran code requires.
> 
> Thank you for the reply and extremely sorry for my late response.
> 
> I have done all these, except from...
> 
> > You also need to write the prototypes for any fortran functions which
> > you wish to call from C.
> 
> ...this.
> 
> My problem is that my app crashes whenever it calls a simple fortran 
> function.
> 
> I've tested the whole stuff with:
> 
> 	PROGRAM FOO 
> 
>         END 
> 
> 	FUNCTION DUMMY (I)
> 	INTEGER I
> 	DUMMY = I               
> 	END
> 
> which I compiled: % g77 bar.f -c -o bar.o
> 					
> and with:
> 
> int main (void)
> {
> 	printf ("%d\n", dummy_ (10);
> 	return 1;
> }
> 
> which I compiled: %gcc foo.c bar.o -lg2c -L/path/to/g2c -o main
> 
> (libg2c is an archive located in: /usr/lib/gcc-lib/i386-redhat-linux/2.96)
> 
> Now, 'main' crashes in dummy_ () and gdb doesn't help, since it produces
> no further stack output. 

Fortran functions are call-by-reference, so you have to pass a pointer
to a C variable:

#include <stdio.h>
#include <g2c.h>

extern integer dummy_(integer *);

int main (void)
{
	integer x = 10;
	printf ("%ld\n", dummy_ (&x));
	return 0;
}

Aslo, you need to declare the function as returning an integer, i.e.:

	INTEGER FUNCTION DUMMY (I)
	INTEGER I
	DUMMY = I
	END

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Calling fortran from C
  2003-04-16 17:42     ` Glynn Clements
@ 2003-04-17 15:23       ` Elias Athanasopoulos
  0 siblings, 0 replies; 5+ messages in thread
From: Elias Athanasopoulos @ 2003-04-17 15:23 UTC (permalink / raw)
  To: Glynn Clements; +Cc: linux-c-programming

Hi Glynn,

On Wed, Apr 16, 2003 at 06:42:57PM +0100, Glynn Clements wrote:
> Fortran functions are call-by-reference, so you have to pass a pointer
> to a C variable:

Thank you very much! :-) It worked and you really saved me a lot of time of
research, since I know almost null about fortran.

Regards,
Elias

-- 
University of Athens			I bet the human brain 
Physics Department				is a kludge --Marvin Minsky 

	

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-04-17 15:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-04-12  8:46 Calling fortran from C Elias Athanasopoulos
2003-04-12 10:15 ` Glynn Clements
2003-04-16 16:53   ` Elias Athanasopoulos
2003-04-16 17:42     ` Glynn Clements
2003-04-17 15:23       ` Elias Athanasopoulos

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).