# Fortran + Julia

**URL:** <https://discourse.julialang.org/t/fortran-julia/2699>\
**Category:** General Usage\
**Tags:** fortran\
**Created:** [March 16, 2017, 3:06pm UTC](https://discourse.julialang.org/t/fortran-julia/2699 "2017-03-16T15:06:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Post date:** [March 16, 2017, 3:06pm UTC](https://discourse.julialang.org/t/fortran-julia/2699/1 "2017-03-16T15:06:48Z")

</div>

I have something in **Fortran** like this:

- `subroutine METHOD` in file `method.f` calling `subroutine eval`;
- `subroutine EVAL` in `main.f`;
- `program MAIN` calling `METHOD`, also in `main.f`.

I can easily call `METHOD` from Julia with `ccall`, but can I define the subroutine `eval` from Julia?

---

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [March 16, 2017, 4:26pm UTC](https://discourse.julialang.org/t/fortran-julia/2699/2 "2017-03-16T16:26:18Z")

</div>

In order to access a Fortran routine from Julia using `ccall`, the Fortran routine needs to be available in a shared library—see the [documentation](http://docs.julialang.org/en/stable/manual/calling-c-and-fortran-code/?highlight=fortran). Could you copy your `EVAL` function into a new file and then compile it into a library? That would seem to me to be the simplest solution.

---

<div class="post-metadata">

**Author:** ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Post date:** [March 16, 2017, 4:34pm UTC](https://discourse.julialang.org/t/fortran-julia/2699/3 "2017-03-16T16:34:40Z")

</div>

Sorry, I wasn’t clear.

I want to implement the subroutine `EVAL` in Julia, so that the Fortran `METHOD` calls that one.

Furthermore, I want to call `METHOD` from Julia using `ccall`, but this last part I can do.

In other words, I want to call a Julia function from a Fortran subroutine called from a Julia function.

---

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [March 16, 2017, 5:01pm UTC](https://discourse.julialang.org/t/fortran-julia/2699/4 "2017-03-16T17:01:14Z")

</div>

Oh, I see. I misunderstood. I’ve never tried to call Julia code from another language but it certainly seems to be possible. As far as I can tell from the [Embedding Julia](http://docs.julialang.org/en/stable/manual/embedding/) page of the documentation, there is a C API that allows calling Julia functions from C. It seems like you need to create a C function that calls your Julia function then use Fortran’s ability to call C functions.

---

<div class="post-metadata">

**Author:** ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Post date:** [March 16, 2017, 5:30pm UTC](https://discourse.julialang.org/t/fortran-julia/2699/5 "2017-03-16T17:30:32Z")

</div>

Thanks, that might work.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [March 17, 2017, 2:02am UTC](https://discourse.julialang.org/t/fortran-julia/2699/6 "2017-03-17T02:02:02Z")

</div>

Fortran [supports C function pointers](https://gcc.gnu.org/onlinedocs/gfortran/Working-with-Pointers.html), so you might be able do this using [`cfunction`](http://docs.julialang.org/en/release-0.5/manual/calling-c-and-fortran-code/#creating-c-compatible-julia-function-pointers) and avoid the C wrapper if you want.

(This would be neat to have as an example, so please post either way if you get something running!)

---

<div class="post-metadata">

**Author:** ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Post date:** [March 17, 2017, 11:38am UTC](https://discourse.julialang.org/t/fortran-julia/2699/7 "2017-03-17T11:38:22Z")

</div>

I think that would require me changing the `METHOD` code, and that’s not available.  
If I make it, I’ll make a post. 👍

---

<div class="post-metadata">

**Author:** ![jibaneza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jibaneza/32/8644_2.png) [@jibaneza](https://discourse.julialang.org/u/jibaneza)\
**Post date:** [January 18, 2019, 1:45pm UTC](https://discourse.julialang.org/t/fortran-julia/2699/8 "2019-01-18T13:45:55Z")

</div>

I know its been a while since you posted, but did you manage to make it work? I am trying to call a FORTRAN subroutine from Julia, where one of the arguments would be a Julia function. I could not find a dummy example online. It would be great to have a simple working example for that.

---

<div class="post-metadata">

**Author:** ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Post date:** [January 18, 2019, 2:16pm UTC](https://discourse.julialang.org/t/fortran-julia/2699/9 "2019-01-18T14:16:18Z")

</div>

I’m afraid not. Calling things in Fortran has been reasonably simple, but I didn’t manage to have Fortran call things in Julia. But I haven’t touched that since Julia 0.6 or 0.5. Maybe you’ll have more luck.

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [January 18, 2019, 4:27pm UTC](https://discourse.julialang.org/t/fortran-julia/2699/10 "2019-01-18T16:27:59Z")

</div>

```julia
julia> fname = tempname() * ".f90"
"/tmp/juliaxmqlRU.f90"

julia> open(fname,"w") do f
           write(f,"subroutine hello(x,z,y)\n")
           write(f," real*8 x,y\n")
           write(f,""" print *, "Hello World! ", x,y, " testing"\n""")
           write(f,"end subroutine hello\n")
       end
21

julia> run(`gfortran -shared -fPIC -o libhello.so $fname`)
Process(`gfortran -shared -fPIC -o libhello.so /tmp/juliaxmqlRU.f90`, ProcessExited(0))

julia> tt = 23.; uu = 12.;

julia> t = ccall( (:hello_, "./libhello"), Int64, (Ptr{Float64},Ptr{Nothing},Ptr{Float64}),Ref(tt),[],Ref(uu))
 Hello World! 23.000000000000000 12.000000000000000 testing
0

julia> 

```
