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

    From Lynn McGuire@lynnmcguire5@gmail.com to comp.lang.fortran on Thu Jan 2 02:27:54 2025
    From Newsgroup: comp.lang.fortran

    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.

    Thanks,
    Lynn

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Thomas Koenig@tkoenig@netcologne.de to comp.lang.fortran on Thu Jan 2 10:06:59 2025
    From Newsgroup: comp.lang.fortran

    Lynn McGuire <lynnmcguire5@gmail.com> 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.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Steven G. Kargl@sgk@REMOVEtroutmask.apl.washington.edu to comp.lang.fortran on Thu Jan 2 18:40:54 2025
    From Newsgroup: comp.lang.fortran

    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

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Lynn McGuire@lynnmcguire5@gmail.com to comp.lang.fortran on Thu Jan 2 14:45:36 2025
    From Newsgroup: comp.lang.fortran

    On 1/2/2025 4:06 AM, Thomas Koenig wrote:
    Lynn McGuire <lynnmcguire5@gmail.com> 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.

    Thanks ! When I was writing the reply to you, I figured out the
    problem. I forgot to put 'extern "C"' in front of the function
    declaration for my fortran code. That fixed my link.

    It is the little things that get you.

    Thanks,
    Lynn

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Lynn McGuire@lynnmcguire5@gmail.com to comp.lang.fortran on Thu Jan 2 16:38:10 2025
    From Newsgroup: comp.lang.fortran

    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 ?

    Lynn

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Steven G. Kargl@sgk@REMOVEtroutmask.apl.washington.edu to comp.lang.fortran on Thu Jan 2 23:37:51 2025
    From Newsgroup: comp.lang.fortran

    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

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Lawrence D'Oliveiro@ldo@nz.invalid to comp.lang.fortran on Fri Jan 3 00:30:39 2025
    From Newsgroup: comp.lang.fortran

    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.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Thomas Jahns@jahns@idontlikespam.dkrz.de to comp.lang.fortran on Thu Jan 9 10:25:10 2025
    From Newsgroup: comp.lang.fortran

    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

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Thomas Koenig@tkoenig@netcologne.de to comp.lang.fortran on Thu Jan 9 10:11:45 2025
    From Newsgroup: comp.lang.fortran

    Thomas Jahns <jahns@idontlikespam.dkrz.de> 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.
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Lynn McGuire@lynnmcguire5@gmail.com to comp.lang.fortran on Fri Jan 10 16:32:27 2025
    From Newsgroup: comp.lang.fortran

    On 1/2/2025 4:06 AM, Thomas Koenig wrote:
    Lynn McGuire <lynnmcguire5@gmail.com> 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.

    BTW, I have thousands of calls from my Fortran code to my C code.
    Adding all of the ISO C binding syntax will take a lot of time. And
    breakage of what was working code using the Watcom Fortran and C compilers.

    Lynn

    --- Synchronet 3.20a-Linux NewsLink 1.114