I'm a C programmer who has to update a huge Fortran 2003 program by adding a single call to a C function.
First, I need to write a minimal Fortran wrapper (in modern, free-form Fortran, no shouting) that will correctly call the C function with a string that contains a loop counter (and the date/time, if possible), from within a loop.
This should be "easy", but none of the searches I've done yielded enough snippets for me to create a working program.
I'm using recent 64-bit versions of gfortran and the Intel ifort compiler under 64-bit Linux, and the test code needs to compile using both compilers.
Here's the C definition, in the file send_to_port.c:
int send_to_port(int port, char *data, unsigned int length);
The last parameter was added to permit Fortran to have to not worry about the trailing null (I handle it in C: data[length] = '\0';). I understand the length parameter is added "automatically" by Fortran, so the Fortran call will have just two parameters, the integer port number and the string to send.
I hope to compile the code with the following gfortran line, plus the equivalent for ifort:
gfortran -ffree-form test.f -o test send_to_port.o
I'm looking for minimal code: I'm thinking it should be around 10-20 lines, but I don't know Fortran. Here's my current edit buffer for test.f (which doesn't compile):
use iso_c_binding
use iso_fortran_env, stdout => output_unit
implicit none
! Fortran interface to C routine:
! int send_to_port(int port, char *data, unsigned int length);
interface
integter(c_int) function send_to_port(port, data) bind(C)
integer(c_int), value :: port
character(kind=c_char) :: data(*)
end interface
integer(c_int) retval, cnt, port
character(1024) str
cnt = 0
port = 5900
do ! Infinite loop (^C to exit)
call fdate(date)
cnt = cnt + 1
write(str, "(A,A,I8)") date, ": Iteration = ", cnt
write(stdout, *) str ! Show what's about to be sent
retval = send_to_port(port, str) ! Send it
write(stdout, *) retval ! Show result
call sleep(1)
end do
end
Help?