免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1038 | 回复: 0
打印 上一主题 下一主题

[SCO UNIX] How to make a C program with FORTRAN ? [复制链接]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-01-05 21:43 |只看该作者 |倒序浏览
How to make a C program with FORTRAN ?

I got the following info from a web site in MIT:

Linking FORTRAN routines with C

When compiling and linking a C program which contains calls to one or
more FORTRAN routines, additional library arguments must be added to the
compile command to enable FORTRAN internal routines to be linked.  Each
Athena workstation type has a different Fortran compiler, with different
Fortran libraries required.  Here is a list of the correct libraries by
machine type:

   Machine type       library arguments

   IBM rs6000          -lxlf
   DEC                 -lfor -li -lutil -lots
   SUN                 -lF77 -lM77 -lV77 -lm -lc (You may also need -lsunmath)
   SGI                 -lI77 -lF77 -lisam (not always necessary)

Calling C from FORTRAN
                                          (path: Top Level/ C Answers/ )

The Unix portable Fortran-77 compiler ('f77') is written almost entirely in C.
The second pass of the compiler is the same one used by the C compiler, and
most 'f77' library routines are simply interfaces to corresponding C library
routines.  However, since Fortran does not support data structures like those
used in C, you may not be able to take advantage of all the functionality that
the 'curses' library offers.  Manipulating windows with 'curses' is especially
difficult.  If you are interested in using routines involving data structures,
you should probably use C instead of 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 this:

In the C source file:

        foo_(bar)       /* See below for information on the underscore! */
        int *bar;       /* Variables are passed by address. */

In the Fortran source file:

        call foo(baz)   /* Assuming that "baz" is an integer. */

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.

If you were calling the 'curses' routine 'move()', you might do
something like this:

The call to the C interface functions are made in the Fortran source file
(named 'test.f'):

        call initscr()
        call clear()
        .
        .
        .
        call move(x, y)
        .
        .
        .
        call refresh()
        call endwin()
        end

...where x and y are integers specifying the new coordinates.

The C source file (named 'curses.c') 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>;

        initscr_()
        {
          initscr();
        }

        clear_()
        {
          clear();
        }

        move_(x, y)
        int *x, *y;             /* These are pointers  */
        {
          move(*x, *y);
        }

        refresh_()
        {
          refresh();
        }

        endwin_()
        {
          endwin();
        }

The routines are compiled by using these commands:

        cc -c curses.c
        f77 test.f curses.o -lcurses -ltermcap

If 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.

For more information, please see the section named "Inter-Procedure Interface"
on pp. 7-8 of the article "A Portable FORTRAN 77 Compiler".  The article is
part of the Unix Programmer's Supplemental Documents, Volume 1, which is in
the Athena Reference Collection in Hayden or Barker libraries (by the public
Athena clusters there), or in the Consultants' Office (11-115).
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP