Re: Proper INTERFACE Syntax for CVF6.6 When Using C Subroutines



There is a generic problem in passing character arrays/strings between
Fortran and C, this is one reason why an interface block is usually a good
idea. In Intel and Compaq/dec Fortran the !dec$ directive allows the exact
attributes of the C routine and its arguments to be described, and this is
best provided in an interface block along with the rest of the declarations.

The interface in your example *may* be correct, and then again it may not
be. That depends on the code of dec_jpeg2000. Do you have any reference
documentation? or even the source? without either of these, we are all left
guessing.

....Ok having just Googled it I find:

http://oceanlab.rsmas.miami.edu/explorer/SEACOOS/v2/src/degrib/src/grib2lib/
g2clib-1.0.2/dec_jpeg2000.c

which declares it as:

int dec_jpeg2000(char *injpc,g2int bufsize,g2int *outfld)

....if this is what you are trying to use then a closer stab at the interface
block might be:


INTERFACE
integer(2) function dec_jpeg2000(cpack,len,ifld)
!DEC$ ATTRIBUTES C, REFERENCE, ALIAS:'_dec_jpeg2000' :: dec_jpeg2000
!dec$ Attributes VALUE: len
character(len=1), intent(in) :: cpack(len)
integer(2) , intent(in) :: len
integer(2) , intent(out) :: ifld(1)
END function dec_jpeg2000
END INTERFACE

>>> NB I have not attempted to compile this <<<

Areas of uncertainy include:

1. The declaration of argument LEN. It looks to me like the routine needs
it by value instead of by reference, I'm not certain of the exact form of
the directive required for this, but the above must surely be close
2. There is no way to specify the length of the IFLD array. The routine
seems to assume the caller (i.e. you) will provide an array big enough to
prevent it being over-indexed. I cannot see a way to determine how big this
needs to be. In any case as far as the interface declaration is concerned,
any length will suffice since the rquirement is to declare it an an array
rather than a scalar.
3. The code I found declares arguments 2 and 3 as type 'g2int', which I
guess is equivalent to a 2-byte integer, and thus I hope the integer kind 2
will be correct.

Note that this is a function not a subroutine (C programmers prefer them) so
instead of CALLing it you will need to do something like

integer(2) :: status
.
.
status = dec_jpeg2000(cpack,len,ifld)

and test the returned value of STATUS.

Hope this helps...

--
Qolin

Email: my qname at domain
Domain: qomputing dot demon dot co dot uk


"tornado Jeff" <jkrob@xxxxxxxxxxx> wrote in message
news:1134425970.124679.37650@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>
> fj wrote:
> > The C signature of dec_jpeg2000 is not clear : is the parameter "len" a
> > pointer to an integer or the value of an integer ?
> >
> > If len is declared "int * len;", then your programming is (almost)
> > right else you should try something like :
> > call dec_jpeg2000(cpack,%VAL(len),ifld)
> >
> > In both cases, the interface is not necessary :
> > - the first case uses the normal F77 argument passing which is the
> > default in F90
> > - the second case uses the %VAL function which is not standard (but
> > very common) and cannot be implemented easily in a F90 interface (we
> > have to wait for a F2003 compiler)
> >
> > Moreover, the interface you wrote seems doubtful because the parameter
> > "ndpts" is not declared
> >
> > The interface by default is equivalent to :
> > INTERFACE doc_jpeg2000
> > SUBROUTINE doc_jpeg2000(cpack,len,ifld)
> > CHARACTER :: cpack(*)
> > INTEGER :: len
> > INTEGER :: ifld(*)
> > END SUBROUTINE
> > END INTERFACE
>
> Thanks for the reply. "len", as stated in the documentation, shows the
> length of the array "cpack". As for "ndpts", it does not need to be
> included in the INTERFACE because it is not passed as an argument to
> the C routine doc_jpeg2000. The CVF (Compaq Visual Fortran)
> documentation says that when passing arguments from a Fortran routine
> to a C routine, there must be an INTERFACE statement to properly define
> the arguments. It talks about simple things like an INTEGER type or
> REAL type but it does not say anything about CHARACTER arrays from what
> I can see. That is why I am searching for help.
>
> Thanks again,
> Jeff
>


.



Relevant Pages

  • public/private attribute before actual declaration
    ... whether the Fortran 95 standard allows to declare ... where the type ODummyThermostat and the interface getInitVelocities ... are declared public before their actual declaration. ...
    (comp.lang.fortran)
  • Re: intel compiler and [REFERENCE] keyword?
    ... Wouldn't that change the binary interface? ... SUBROUTINE SYSFNCBIND ... This is trouble - Fortran LOGICAL does not match ... This binary interface has been stable for about 10 years and has been ...
    (comp.lang.fortran)
  • Re: getting env. variables in gfortran...
    ... Fortran standard (processes, threads, priorities, interprocess ... POSIX functions and Fortran 77. ... POSIX standard itself, not just to the fortran 77 interface. ...
    (comp.lang.fortran)
  • Re: F90 to C interface
    ... I had a while ago a similar problem (interfacing Fortran code with C ... libraries); theoretically you would be able to use the Fortran2003 ... A Fortran module was also written containing interface ... Regarding arrays, you have to be careful: ...
    (comp.lang.fortran)
  • Re: [F03 C-interoperability] Correct way to do C_PTR (char *) to CHARACTER vector conversion
    ... I am planning to embark on a project in Fortran 95. ... that I needed to interface against zlib to compress data. ... FUNCTION ZLIBVERSION BIND ...
    (comp.lang.fortran)