The content of this document may be incorrect or outdated.
Print this article Edit this article
Calling c from fortran
To call C routines from a Fortran program, you will have to write some C code. Fortran passes arguments by reference or address, so the C function has to be prepared to accept the variable as an address. This means that you will have to write functions in C that are called from Fortran that set up the arguments properly before calling the library function. Schematically, this might be something like:
Note underscore ( _ ) in name of routine.
In the C source file:
foo_(bar)In the Fortran source file:
/* Note underscore */
int *bar;
/* Note variables are passed by address */
{
:
}
call foo(baz)The underscore is important because Fortran uses the character to keep its symbols straight. If you are compiling on an RS6000 this is not default behavior, but compiling with the -qextname option on the RS6000 will cause it to perform identical to the other platforms.
/* Assuming that "baz" is an integer. */
{
:
}
Calling C From Fortran
Note the Fortran example below and how it calls C routines:
call initscr()...where x and y are integers specifying the new coordinates.
call clear()
.
.
.
call move(x, y)
.
.
.
call refresh()
call endwin()
end
Calling Curses from Fortran
If you were calling the 'curses' routine 'move()', you might do something like this:
The C source file contains the interface routine to the 'curses' library function 'move()', along with the other C functions that provide an interface to the some other 'curses' functions:
#include <curses.h>The routines are compiled by using these commands:
initscr_()
{
initscr();
}
clear_()
{
clear();
}
move_(x, y)
int *x, *y; /* These are pointers */
{
move(*x, *y);
}
refresh_()
{
refresh();
}
endwin_()
{
endwin();
}
cc -c curses.cIf you are using macros defined in /usr/include/curses.h in your Fortran file, be warned that they assume conventions of the C language. Be aware that this may affect the results you obtain when using them in Fortran.
f77 test.f curses.o -lcurses -ltermcap
Last Modified:
Dec 19, 2016 11:12 am US/Eastern
Created:
May 31, 2007 1:20 pm GMT-4
by
admin
JumpURL: