Re: using iso_c_binding



"Brendan" <brendandetracey@xxxxxxxxx> wrote in message
news:a8f7c1c7-d950-44e7-9f09-012510e88695@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Thanks for the indepth replies. I stated in another thread that I do
not know C and have yet to make the leap from 77 to 90/95/2003(got
side-tracked by Matlab), so I am seriously short-handed. I did get
the code to compile with the -m32 flag only to get errors popping out
of the C library to which I am trying to link.

It's a frustration over on this side not to be able to see your current
Fortran code nor the point (compilation, linking, or at runtime) that
you are now experiencing problems, nor the exact error statement...

I guess I better order a copy of 95/2003 explained. I started reading
a 90 text last night...

I have earlier editions of this series (like Fortran 95 Explained)
and they are really good books. At least the earlier editions were
thin enough that you could do a light read of most of the book in
a couple of days after which, although you couldn't possibly get
exact knowledge of all the features of modern Fortran in such a
brief time span, at least you knew roughly what they were and where
to find them in the book.

I just use N1601.pdf for a reference. The chapter on C interop is
short. If you do not know C, you should be aware that it's just an
obfuscated form of PDP-11 assembly and that assembly language is
much easier to learn. I always think about the C interop features
in terms of the machine code that the compiler is producing. With
gfortran and gcc, you can see the machine code by compiling with
the -S switch (this produces an assembly language listing but no
object file or executable) and examining the *.s file the compiler
makes for you. You can synthesize the C procedure you are trying
to link to in Fortran as well:

subroutine Aps_MinMax(A, N, A_MIN, A_MAX) bind(C, NAME='Aps_MinMax')
use ISO_C_BINDING
implicit none
type(C_PTR), value :: A
integer(C_INT), value :: N
type(C_PTR), value :: A_MIN
type(C_PTR), value :: A_MIN
real(C_DOUBLE), pointer :: p_A(:)
real(C_DOUBLE), pointer :: p_A_MIN
real(C_DOUBLE), pointer :: p_A_MAX

call C_F_POINTER(A, p_A, [N])
! You might not want the min
if(C_ASSOCIATED(A_MIN)) then
call C_F_POINTER(A_MIN, p_A_MIN)
p_A_MIN = minval(p_A)
end if
! You might not want the max
if(C_ASSOCIATED(A_MAX)) then
call C_F_POINTER(A_MAX, p_A_MAX)
p_A_MAX = maxval(p_A)
end if
end subroutine Aps_MinMax

This gives you a procedure you can try to link to, but written
in Fortran. I do this a lot and it helps to understand the
issues involved in interfacing to the C procedure.

--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end


.



Relevant Pages

  • Re: Link object file from C++ to Fortran 77
    ... Fortran 77. ... the compiler will write a non-corrupt object file. ... Link your Fortran program against that DLL ...
    (comp.lang.fortran)
  • Link object file from C++ to Fortran 77
    ... Fortran 77. ... the compiler will write a non-corrupt object file. ... Disable minimal rebuild with the /Gm- ...
    (comp.lang.fortran)
  • Re: A question on Newtons Method
    ... >> I can't believe you'd advise a noob to use a compiler which is still ... > production codes and codes from textbooks. ... > The Fortran standard generally does not specify the required behavior ... >> another important class of hassle which most numerical programmers ...
    (sci.math.num-analysis)
  • Fortran Resources (July 2004)
    ... and the standard for the Fortran language and its derivatives. ... WHERE CAN I OBTAIN A FORTRAN 95 COMPILER? ... Absoft Fortran compilers, debuggers, and development tools for Windows, ... are available for Linux systems. ...
    (comp.lang.fortran)
  • Fortran Resources (August 2004)
    ... and the standard for the Fortran language and its derivatives. ... Absoft Fortran compilers, debuggers, and development tools for Windows, ... and Linux include source-compatible Fortran 95 compiler suites ... are available for Linux systems. ...
    (comp.lang.fortran)