# Calling Fortran shared library in Julia always returns 0.0

**URL:** <https://discourse.julialang.org/t/calling-fortran-shared-library-in-julia-always-returns-0-0/117239>\
**Category:** New to Julia\
**Created:** [July 19, 2024, 2:08pm UTC](https://discourse.julialang.org/t/calling-fortran-shared-library-in-julia-always-returns-0-0/117239 "2024-07-19T14:08:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tianyi\_zhang](https://avatars.discourse-cdn.com/v4/letter/t/da6949/32.png) [@tianyi\_zhang](https://discourse.julialang.org/u/tianyi_zhang)\
**Post date:** [July 19, 2024, 2:08pm UTC](https://discourse.julialang.org/t/calling-fortran-shared-library-in-julia-always-returns-0-0/117239/1 "2024-07-19T14:08:47Z")

</div>

Hello, everyone in the Julia discourse community, this is my first post here and I’m having a problem with calling Fortran in Julia.

The Fortran code I’m calling can be downloaded [here](https://www.science.org/doi/suppl/10.1126/sciadv.adj8632/suppl_file/sciadv.adj8632_codes_s1_and_s2.zip), which is a scientific code.  
What I changed is to add this function to the file adj8632\_Code\_S1.f90:

```julia
real*8 function pot_H2CO_V23(R, th1, th2, phi, rh2, rco)
    implicit none
    real*8,intent(in) :: R, th1, th2, phi, rh2, rco
    real*8 :: eint
    call pot_H2CO6D_V23(R, th1, th2, phi, rh2, rco, eint)
    pot_H2CO_V23 = eint
    return
end function pot_H2CO_V23

```

which is what I will be calling from Julia.

Then I compiled adj8632\_Code\_S1.f90 with  
`ifort -shared -fPIC adj8632_Code_S1.f90 -o adj8632_Code_S1.so`, and then called from Julia:

```julia
function h2copes(R, θ1, θ2, ϕ, rh2, rco)
    eint = @ccall "path to library".:pot_h2co_v23_(R::Ref{Float64}, θ1::Ref{Float64}, θ2::Ref{Float64}, ϕ::Ref{Float64}, rh2::Ref{Float64}, rco::Ref{Float64})::Float64
    return eint
end
h2copes(8., 0., 180., 0., 1.474, 2.165)

```

now no matter what my input is, it always returns `0.0`. And I don’t know what’s wrong.

I have used this approach with other Fortran codes, no problem has occured.

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [July 19, 2024, 2:36pm UTC](https://discourse.julialang.org/t/calling-fortran-shared-library-in-julia-always-returns-0-0/117239/2 "2024-07-19T14:36:46Z")

</div>

Have you verified that the fortran subroutine `pot_H2CO6D_V23` receives the right input, and that `eint` is different from `0.0` (e.g. with a `print *`, or similar.

---

<div class="post-metadata">

**Author:** ![tianyi\_zhang](https://avatars.discourse-cdn.com/v4/letter/t/da6949/32.png) [@tianyi\_zhang](https://discourse.julialang.org/u/tianyi_zhang)\
**Post date:** [July 19, 2024, 3:09pm UTC](https://discourse.julialang.org/t/calling-fortran-shared-library-in-julia-always-returns-0-0/117239/3 "2024-07-19T15:09:02Z")

</div>

The input is correct and the Fortran function runs correctly in the Fortran code.

This is what’s eating at me: the function runs in the fortran file but not when called interoperably from Julia.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 19, 2024, 3:27pm UTC](https://discourse.julialang.org/t/calling-fortran-shared-library-in-julia-always-returns-0-0/117239/4 "2024-07-19T15:27:13Z")

</div>

Try changing the return value into an output argument, i.e. change the function into a `subroutine`.

(I’ve had problems in the past with Fortran ABI calling conventions for function return values not matching C calling conventions; not sure if that’s an issue with `ifort` on your platform.)

---

<div class="post-metadata">

**Author:** ![tianyi\_zhang](https://avatars.discourse-cdn.com/v4/letter/t/da6949/32.png) [@tianyi\_zhang](https://discourse.julialang.org/u/tianyi_zhang)\
**Post date:** [July 20, 2024, 2:59am UTC](https://discourse.julialang.org/t/calling-fortran-shared-library-in-julia-always-returns-0-0/117239/5 "2024-07-20T02:59:07Z")

</div>

It turns out that I didn’t include the necessary parameters to run the subroutine ` pot_H2CO6D_V23` 😅.

If you are interested, you need to call these subroutines:

```julia
call init_data_old
call init_data_new

```

they contain the parameters.

Thank you all for the responses, the Julia community is really helpful.
