..
interface
function twice(i) bind(c)
use, intrinsic :: iso_c_binding
integer(c_int) :: i
integer(c_int) :: twice
end function twice
end interface
..
On Monday, February 21, 2022 at 6:47:46 PM UTC-5, Beliavsky wrote:
..
interface
function twice(i) bind(c)
use, intrinsic :: iso_c_binding
integer(c_int) :: i
integer(c_int) :: twice
end function twice
end interface
..
The VALUE attribute is missing in the dummy argument 'i' in the interface block.
For the C code in twice.c
int twice(int i)
{
return 2*i;
}
For the C code in twice.c
int twice(int i)
{
return 2*i;
}
and Fortran code in cfunc.f90
program main
use iso_c_binding, only: c_int
implicit none
integer(c_int) :: i
interface
function twice(i) bind(c)
use, intrinsic :: iso_c_binding
integer(c_int) :: i
integer(c_int) :: twice
end function twice
end interface
i = 5
print*,i,twice(i)
end program main
"Beliavsky" wrote in message news:d5312537-6c84-4eaa-be4b-3d26d6253d2en@googlegroups.com...
In my Fortran codes I specify the KIND of real variables but not integer variables. For compatibility with C it is
safer to declare integers as integer(c_int), but are there platforms where such an integer is distinct from the
default integer? In my code above if I replace integer(c_int) with integer and get rid of the "use iso_c_binding" statements,
it still works.
For the C code in twice.c
int twice(int i)
{
return 2*i;
}
and Fortran code in cfunc.f90
program main
use iso_c_binding, only: c_int
implicit none
integer(c_int) :: i
interface
function twice(i) bind(c)
use, intrinsic :: iso_c_binding
integer(c_int) :: i
integer(c_int) :: twice
end function twice
end interface
i = 5
print*,i,twice(i)
end program main
I compile and run on Windows with
del *.o
if exist a.exe del a.exe
gcc -c twice.c
gfortran -c cfunc.f90
gfortran -o a.exe twice.o cfunc.o
if exist a.exe a.exe
but the output is
5 1182790712
What did I do wrong?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 159 |
Nodes: | 16 (0 / 16) |
Uptime: | 98:50:27 |
Calls: | 3,209 |
Files: | 10,563 |
Messages: | 3,009,783 |