• How can Fortran call a C library function without explicitly linking to

    From Beliavsky@21:1/5 to All on Wed Feb 23 11:24:57 2022
    The command

    gfortran xatoi.f90

    where xatoi.f90 has the code below creates an executable. This is neat, but I am surprised that I don't need to use a C compiler. Where is the function atoi coming from? For a C program (also listed below) there is a line

    #include <stdlib.h>

    to enable calls to atoi. In general, is an INTERFACE all you need to call a function in the C standard library https://www.csse.uwa.edu.au/programming/ansic-library.html ?

    ! from "Interoperation of Fortran with C"
    program main
    use, intrinsic :: iso_c_binding, only: c_char, c_null_char
    implicit none
    function atoi(in) bind(c)
    use, intrinsic :: iso_c_binding
    integer(c_int) :: atoi
    character(c_char) :: in(*)
    end function
    end interface
    integer :: i
    character(len=:,kind=c_char), allocatable :: digits
    allocate(character(len=5) :: digits)
    digits = c_char_'1234' // c_null_char
    i = atoi(digits) ! i gets set to 1234
    end program main

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