Gfortran has a bug that prevents using aliases via “USE, ONLY” as done here
That page was last updated on January 18, 2012. I am able to compile the code shown with gfortran 13.3.0 and also a later version, so has the bug been fixed? If so, what was the earliest version of gfortran that fixed it? I have not used C interoperability much – could someone review this page and edit it as needed?
I came across that module not so long ago while looking for a memcpy interface. It’s quite useful to have such module since I always endup rewriting the same interfaces to memcpy, fputs, fgets, abort, etc., over and over again @Arjen, while you are at it, you could maybe mention that it does not compile with lfortran yet.
Last time I checked, lfortran was missing some constants in it’s iso_c_binding module like C_INTPTR_T or the C_INT_FAST.
It’s probably not to much of an effort to make this module compilable with all (?) compilers at the expense of some macros. I did not try though.
I tried compiling the module as is with the following compilers and versions:
Linux: gfortran 8.5.0, 12.2.0, 13.2.0
Windows: gfortran 14.1.0, ifx 2025.0.0
All of them accepted the code. With extra checks on (–warn-all and -check:all) I got two warnings:
c_interface.f90:419:20:
419 | forall (i=1:strlen)
| 1
Warning: Possible change of value in conversion from INTEGER(8) to INTEGER(4) at (1) [-Wconversion]
and:
c_interface.f90(332): warning #6706: There is a mixture of specific functions and specific subroutines for one generic-spec. [F_C_STRING_PTR]
subroutine F_C_string_ptr(F_string, C_string, C_string_len)
-------------^
Nothing more. I have not checked against lfortran yet, but it seems safe to assume that most compilers will accept the code. Note that my tests do not yet include actually using the routines.
Okay, I installed lfortran version 0.45.0. After adding definitions for a large number of kinds, I finally stopped at:
semantic error: Type mismatch in argument at argument (1); passed `type(c_ptr)` to `c_char_ptr`.
--> c_interface.f90:392:33
|
392 | character(len=C_strlen_safe(C_string)) :: F_string
| ^^^^^^^^
lfortran is the only compiler that makes a difference between c_char_ptr and c_ptr. Changing the type to c_char_ptr brougth up the next hurdle: the intrinsic function c_associated() is not defined.
I guess I should report this at the lfortran github site.