Ccall problem while calling fortran

Hi all,

I was playing with calling a stupid fortran code in julia. I encountered some weird results.

1)Here is my fortran code to calculate “volume”.

module volmod
    implicit none

    
contains
    function volume(x,y,z) result(vol)
        !DEC$ ATTRIBUTES DLLEXPORT :: volume
        use, intrinsic :: iso_fortran_env, only: RK => real64
        real(RK), intent(in) :: x,y,z
        real(RK)             :: vol
        vol = x*y*z
    end function volume
end module volmod

I, then, created a shared library with

ifort /dll square3.f90

I called this function in julia using

function volum(DX, DY,DZ)

       product = ccall(("VOLMOD_mp_VOLUME", "C:/Users/<user>/Desktop/recycling_bin/module/square3.dll"),
                    Float64,
                    (Ref{Float32},Ref{Float32},Ref{Float32}),
                    DX,DY,DZ)
    return product
end

rt=volum(4.0,4.0,3.0)

It returns 0. If I change the fortran code to

module volmod
    implicit none

    
contains
    function volume(x,y,z) result(vol)
        !DEC$ ATTRIBUTES DLLEXPORT :: volume
        use, intrinsic :: iso_fortran_env, only: RK => real64
        real(RK), intent(in) :: x,y,z
        real(RK)             :: vol
        vol = 100
    end function volume
end module volmod

The julia code returns 100 as expected. Any ideas, what is wrong with the first code?

  1. Separate from the above issue, whenever I ccall a fortran function that requires a single input argument, I always receive the following error
LoadError: syntax: ccall argument types must be a tuple; try "(T,)" around c:\Users\<user>\Desktop\recycling_bin\module\julia\square.jl:7

“”

Basically, ccall requires me to pass at least two input arguments. Any ideas?

  1. Finally, although julia documentation says " (:function, "library") and ("function", "library")" are the same for ccall. :function does not work in my case.

----I am using Julia 1.6 on windows 10 in case it is helpful.

Blockquote The julia code returns 100 as expected. Any ideas, what is wrong with the first code?

I figured out my error. Input type must have been Ref{Float64} not 32. That answers my first question.

The error message is telling you.

LoadError: syntax: ccall argument types must be a tuple; try "(T,)" around c:\Users\<user>\Desktop\recycling_bin\module\julia\square.jl:7

Although your function only has one argument, the Julia interface requires the argument typess to be a tuple. So in this case you need a one-tuple

ccall(("function_name", "dll_to_link_to"),
       Float64,
       (argument_type,), argument)

instead of

ccall(("function_name", "dll_to_link_to"),
       Float64,
       (argument_type), argument)

(argument_type) is not a tuple, (argument_type,) is a tuple. EDIT: In case it’s not obvious, there’s an extra comma in the expression that is a tuple.

Sorry I can’t help with your other questions.

1 Like

Thank you sir! This helps a lot!

1 Like