Trying to bind C's rand but i cant get it to work

this is the code

module cfunctions
    use, intrinsic :: iso_c_binding, only : c_int
    interface
        integer(c_int) function c_rand() bind(C, "rand")
            use, intrinsic :: iso_c_binding, only : c_int
        end function rand
    end interface
end module cfunctions

and these are the errors

src/main.f90:4:16:

    4 |         integer(c_int) function c_rand() bind(C, "rand")
      |                1
Error: Parameter ‘c_int’ at (1) has not been declared or is a variable, which does not reduce to a constant expression
src/main.f90:5:57:

    5 |             use, intrinsic :: iso_c_binding, only : c_int
      |                                                         1
Error: Unexpected USE statement in INTERFACE block at (1)
src/main.f90:6:11:

    6 |         end function rand
      |           1
Error: Expecting END INTERFACE statement at (1)

Welcome to the Discourse!

Inside the interface, try replacing use, intrinsic :: iso_c_binding, only : c_int by:

import :: c_int

See fortran - IMPORT statement within module procedures - Stack Overflow

1 Like

there are 2 instances of that code in my source, which one/s do i replace?

The one inside the interface block (the second one).

more errors

[yeti:~/dev/f/snake] make
gfortran -I./M_ncurses/src/ src/main.f90 -Wall -Wextra -Werror -pedantic -std=f2003 ./M_ncurses/src/libm_ncurses.a -lncurses -o snake
src/main.f90:4:16:

    4 |         integer(c_int) function c_rand() bind(C, "rand")
      |                1
Error: Parameter ‘c_int’ at (1) has not been declared or is a variable, which does not reduce to a constant expression
src/main.f90:5:19:

    5 |             import :: c_int
      |                   1
Error: IMPORT statement at (1) only permitted in an INTERFACE body
src/main.f90:6:11:

    6 |         end function rand
      |           1
Error: Expecting END INTERFACE statement at (1)

Make sure to have matching names or omit the names in the end ... statements.

thanks

See your other post

Please don’t forget the asinine need with the not-yet-fully modern Fortran to include the implicit none statement in all your program units and each of your interface bodies! :sob: Or if you’re really brave, eschew this relic but go with compiler options such as -fimplicit-none though I wouldn’t recommend to be this daring yet in production code.

module cfunctions
   use, intrinsic :: iso_c_binding, only : c_int
   implicit none
   interface
      function c_rand() result(r) bind(C, name="rand")
         import :: c_int
         implicit none
         ! function result
         integer(c_int) :: r
..

Why does it require bravery and daring to use -fimplicit-none? That option creates a better dialect of Fortran. Do you not trust the compiler to implement it correctly or something?

As things stand right now with everything with Fortran across multiple processors, should a production codebase not explicitly include impliicit none statements then it is obviously risky to rely on a compiler-specific option only such as -fimplicit-none with gfortran. Given the implicit mapping scheme in the standard with I-N starting letters on object names rendering them default integer types and all else as default real can cause havoc to state the least.

A standard either means something or it doesn’t. I believe the way to address the nonsense with implicit none in Fortran is via the standard itself, not compiler options, nor fpm, neither LFortran, etc.

I do think Fortran 202Y standard revision, which practically can only come into effect at the earliest circa 2035, should consider making implicit none the default. One relatively simpler way to do so might be to eliminate implicit mapping once and for all.

gfortran is not the only compiler with an option that does that. Obviously, if you want to develop Fortran using a compiler that doesn’t support that feature you shouldn’t use it.

implicit none may someday become the default in Fortran, but I doubt it will happen in my lifetime. Fortunately, with a reasonably modern compiler I don’t have to wait for it.

Duh! Exactly why I stated upthread it will be daring to eschew implicit none statements at this point in time. There are compiler implementations which are too much into supporting the implicit mapping from the first incarnation of FORTRAN.

With the next commonly used Fortran processor following gfortran, it takes a couple of options to get forceful support from the compiler with implicit none and the process will be painful for the other warnings will end up being errors also.

   i = 42
end
C:\temp>ifort /c /warn:declarations /warn:errors p.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.5.0 Build 20211109_000000
Copyright (C) 1985-2021 Intel Corporation.  All rights reserved.

p.f90(1): error #6717: This name has not been given an explicit type.   [I]
   i = 42
---^
compilation aborted for p.f90 (code 1)