• Re: how do you send a fortran character string from GCC to GFortran ?

    From Thomas Koenig@21:1/5 to Lynn McGuire on Thu Jan 2 10:06:59 2025
    Lynn McGuire <[email protected]> schrieb:
    How do you send a fortran character string from GCC to GFortran ?

    I cannot get this to link. I can do the reverse, send a fortran
    character string from Gfortran to GCC.

    A full, self-contained example would be helpful for somebody trying to
    help (especially since you say "link", which seems weird).

    But take a look at

    https://gcc.gnu.org/onlinedocs/gfortran/Naming-and-argument-passing-conventions.html

    I do have the additional complication that I do not know the length of
    the fortran character string being sent from GCC to Gfortran at compile
    time, only run time. So that is a character*(*) string.

    I am not using the ISO C binding.

    It is generally a good idea to use ISO C binding in new code, it
    is what it was introduced for.

    But you might also find

    https://gcc.gnu.org/onlinedocs/gfortran/Interoperability-Options.html

    of interest.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Steven G. Kargl@21:1/5 to Lynn McGuire on Thu Jan 2 18:40:54 2025
    On Thu, 02 Jan 2025 02:27:54 -0600, Lynn McGuire wrote:

    How do you send a fortran character string from GCC to GFortran ?

    I cannot get this to link. I can do the reverse, send a fortran
    character string from Gfortran to GCC.

    I do have the additional complication that I do not know the length of
    the fortran character string being sent from GCC to Gfortran at compile
    time, only run time. So that is a character*(*) string.

    I am not using the ISO C binding.

    As Thomas as indicated, ISO C binding was introduced into the
    Fortran standard to address your needs. But, if you want to
    go old school with gcc/gfortran, then

    % cat aa.c
    #include <string.h>

    void
    string_(char *s, int *slen)
    {
    strncpy(s, "abc", *slen);
    }

    % cat bb.f90
    program foo
    external :: string
    character(len=10) str
    call string(str, len(str))
    print *, '>>' // str //'<<'
    end program foo

    % ~/work/bin/gcc -c aa.c
    % gfcx -o z bb.f90 aa.o
    % ./z
    abc<<

    --
    steve

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Steven G. Kargl@21:1/5 to Lynn McGuire on Thu Jan 2 23:37:51 2025
    On Thu, 02 Jan 2025 16:38:10 -0600, Lynn McGuire wrote:

    On 1/2/2025 12:40 PM, Steven G. Kargl wrote:
    On Thu, 02 Jan 2025 02:27:54 -0600, Lynn McGuire wrote:

    How do you send a fortran character string from GCC to GFortran ?

    I cannot get this to link. I can do the reverse, send a fortran
    character string from Gfortran to GCC.

    I do have the additional complication that I do not know the length of
    the fortran character string being sent from GCC to Gfortran at compile
    time, only run time. So that is a character*(*) string.

    I am not using the ISO C binding.

    As Thomas as indicated, ISO C binding was introduced into the
    Fortran standard to address your needs. But, if you want to
    go old school with gcc/gfortran, then

    % cat aa.c
    #include <string.h>

    void
    string_(char *s, int *slen)
    {
    strncpy(s, "abc", *slen);
    }

    % cat bb.f90
    program foo
    external :: string
    character(len=10) str
    call string(str, len(str))
    print *, '>>' // str //'<<'
    end program foo

    % ~/work/bin/gcc -c aa.c
    % gfcx -o z bb.f90 aa.o
    % ./z
    >>abc<<

    Isn't the character string length variable "slen" a value parameter and size_t type ?


    'int *' is a pointer to an int. size_t may or may not be an int.

    If one refuses to use the facilities of the Fortran standard,
    namely ISO C binding, then one needs to experiment to determine
    the type(s) and calling convention.

    --
    steve

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Lynn McGuire on Fri Jan 3 00:30:39 2025
    On Thu, 2 Jan 2025 02:27:54 -0600, Lynn McGuire wrote:

    How do you send a fortran character string from GCC to GFortran ?

    Seems like trying to call Fortran routines from C is not quite
    straightforward. Looking at the (draft?) Fortran 2023 spec, page 514 says:

    In a reference from C to a Fortran procedure with an interoperable
    interface, a C actual argument shall be the address of a C
    descriptor for the intended effective argument if the
    corresponding dummy argument interoperates with a C formal
    parameter that is a pointer to CFI_cdesc_t.

    The following section (page 516 onwards) defines these “C descriptors”. Or alternatively (back to page 514), you might need to declare your Fortran routine with the “BIND(C)” attribute.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Jahns@21:1/5 to Lynn McGuire on Thu Jan 9 10:25:10 2025
    On 2025-01-02 23:38, Lynn McGuire wrote:
    Isn't the character string length variable "slen" a value parameter and size_t
    type ?

    Depends: gfortran 8 and subsequent versions use size_t, older versions and other
    compilers use int AFAIK.

    I guess, handling strings beyond 2GB is not a particular strength of Fortran anyway, so I don't know why that change was made.

    Thomas

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Koenig@21:1/5 to Thomas Jahns on Thu Jan 9 10:11:45 2025
    Thomas Jahns <[email protected]> schrieb:
    On 2025-01-02 23:38, Lynn McGuire wrote:
    Isn't the character string length variable "slen" a value parameter and size_t
    type ?

    Depends: gfortran 8 and subsequent versions use size_t, older versions and other
    compilers use int AFAIK.

    I guess, handling strings beyond 2GB is not a particular strength of Fortran anyway, so I don't know why that change was made.

    There should be as few artificial limitations in a compiler as possible.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)