Discussion:
how do you send a fortran character string from GCC to GFortran ?
Add Reply
Lynn McGuire
2025-01-02 08:27:54 UTC
Reply
Permalink
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
Thomas Koenig
2025-01-02 10:06:59 UTC
Reply
Permalink
Post by Lynn McGuire
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
Post by Lynn McGuire
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.
Lynn McGuire
2025-01-02 20:45:36 UTC
Reply
Permalink
Post by Thomas Koenig
Post by Lynn McGuire
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
Post by Lynn McGuire
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
Lynn McGuire
2025-01-02 20:46:05 UTC
Reply
Permalink
Post by Thomas Koenig
Post by Lynn McGuire
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
Post by Lynn McGuire
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
Lynn McGuire
2025-01-10 22:32:27 UTC
Reply
Permalink
Post by Thomas Koenig
Post by Lynn McGuire
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
Post by Lynn McGuire
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

Steven G. Kargl
2025-01-02 18:40:54 UTC
Reply
Permalink
Post by Lynn McGuire
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
Post by Lynn McGuire
abc<<
--
steve
Lynn McGuire
2025-01-02 22:38:10 UTC
Reply
Permalink
Post by Steven G. Kargl
Post by Lynn McGuire
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
Post by Lynn McGuire
abc<<
Isn't the character string length variable "slen" a value parameter and
size_t type ?

Lynn
Steven G. Kargl
2025-01-02 23:37:51 UTC
Reply
Permalink
Post by Lynn McGuire
Post by Steven G. Kargl
Post by Lynn McGuire
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
Post by Lynn McGuire
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
Thomas Jahns
2025-01-09 09:25:10 UTC
Reply
Permalink
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
Thomas Koenig
2025-01-09 10:11:45 UTC
Reply
Permalink
Post by Thomas Jahns
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.
Lawrence D'Oliveiro
2025-01-03 00:30:39 UTC
Reply
Permalink
Post by Lynn McGuire
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.
Loading...