(j3.2006) A few constants from ISO_Fortran_env for C interop

Van Snyder Van.Snyder
Mon Oct 24 19:34:44 EDT 2011


On Mon, 2011-10-24 at 15:51 -0700, Bill Long wrote:
> 
> On 10/24/11 2:53 PM, Van Snyder wrote:
> >
> > On Mon, 2011-10-24 at 12:00 -0700, Bill Long wrote:
> >> Alternatively, a user-written module, that has a USE of
> >> ISO_Fortran_env, could be supplied that declared bind(c) variables
> >> that were initialized to these constants.  Then the C user could just
> >> reference the variables directly.   You might name them Fortran_stdout
> >> and Fortran_stderr, for example.
> >
> > The variables aren't constants that a C function can use for
> > initialization.  So this is a little better than (or not quite as good
> > as, depending upon your viewpoint) using a BIND(C) subroutine to set the
> > values.
> >
> 
> I don't see how initialization in C is relevant here.  The C programmer 
> is only going to pass these values back to a Fortran program as actual 
> arguments in calls.  The numerical values of the unit numbers are 
> pointless to a C programmer.

They are pointless if C does the I/O, and necessary if Fortran is going
to do the I/O.  The C program isn't going to pass them "back" if the
main program is in C and the only Fortran procedure is the one that
wants to know on which unit to do I/O.

> > Fortran programmers have maybe been spoiled by having default
> > initialization.
> 
> And also lead to poor programming practice through excessive use of 
> default initialization. Particularly of components.

Beauty is in the eye of the beholder.

> > I don't see enough C code to know how comfortable the average C
> > programmer is with instructions to call several initialization functions
> > before calling the one that does what he actually wants done.  Is it
> 
> The global variables avoid that problem.  No initialization functions 
> involved.

Assignment statements instead, if their values (not their addresses) are
needed in struct members.

Suppose I have in Fortran
  type, bind(c) :: MyType
    ....
    integer(c_int) :: IO_Unit
    ....
  end type MyType

Then on the C side I have
  typedef struct { ... int IO_unit ... } MyType;

  MyType myThing = { ... ????? ... };

What do I put in place of ????? to set the IO_Unit member to a value
that would cause the called Fortran procedure to use OUTPUT_UNIT or
ERROR_UNIT?

I either need to have
  extern int Fortran_stdout, Fortran_stderr;
  myThing.IO_unit = Fortran_stdout; /* or Fortran_stderr */

or call some Fortran routine
  initialize_MyType ( &myThing );

or write a Fortran program that writes a .h file having #defines in it
for INPUT_UNIT, OUTPUT_UNIT and ERROR_UNIT and #include that file to get
the name to put in place of ????? (making sure to include compiling and
running that program as part of "make")

before

  Fortran_Subroutine ( ... &myThing ... );

> Cheers,
> Bill
> 
> 
> > common and tolerated, and recognized that failing to do the
> > initialization properly is a source for obscure bugs?
> >
> >
> > _______________________________________________
> > J3 mailing list
> > J3 at j3-fortran.org
> > http://j3-fortran.org/mailman/listinfo/j3
> 




More information about the J3 mailing list