Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!uwm.edu!ogicse!ucsd!network.ucsd.edu!weber.ucsd.edu!corrigan From: corrigan@weber.ucsd.edu (Michael J. Corrigan) Newsgroups: comp.lang.fortran Subject: Re: How to call a fortran subroutine in a C program? Message-ID: <5054@network.ucsd.edu> Date: 27 Mar 91 11:05:50 GMT References: <2773@pacific.mps.ohio-state.edu> Sender: news@network.ucsd.edu Organization: Division of Social Sciences, UCSD Lines: 30 Nntp-Posting-Host: weber.ucsd.edu In article <2773@pacific.mps.ohio-state.edu> sun@pacific.mps.ohio-state.edu (Mingqiu Sun) writes: > >This question is probably asked before. Is there an easy way to call >a subroutine written in fortran by a C program? I am talking about ... Some of the principles involved are : 1. You must pass only pointers from C to the fortran, since as you know fortran expects to be able to change the value of a variable when it is passed to a subroutine Principle is C is call-by-value, Fortran is call-by-reference 2. In some cases the order of the arguments must be reversed, between the caller and the callee, sometimes not ( implementation dependent) (easy to check) Principle is the order in which the arguments to a function are pulled of the stack is compiler dependent ( and not required to be consistent between languages/compilers since such things are "internal", I suppose) Some of the other facts/tricks are: 3. in some cases you must name the fortran subroutine sub_ in the C call since the original fortran compiler that compiled the, say IMSL, libraries would have added that character to the name that you would think it is (really easy to check - the linker says can't find aaa_) 4. Sometimes you can't easily make the call directly but must call a helper/wrapper routine in one language or the other to straighten things out ( at least if you wish to preserve some appearance of semantics) Feel free to email me if you can't make it work or want an example. Michael J. Corrigan corrigan@ucsd.edu