# Fortran calling Julia

**URL:** https://fortran-lang.discourse.group/t/fortran-calling-julia/6714
**Category:** Help
**Created:** [October 31, 2023, 3:08pm UTC](https://fortran-lang.discourse.group/t/fortran-calling-julia/6714 "2023-10-31T15:08:05Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![hkershaw](https://avatars.discourse-cdn.com/v4/letter/h/9dc877/32.png) [@hkershaw](https://fortran-lang.discourse.group/u/hkershaw)
#### Post date: [October 31, 2023, 3:08pm UTC](https://fortran-lang.discourse.group/t/fortran-calling-julia/6714/1 "2023-10-31T15:08:05Z")

</div>

Looking for advice/experience/pitfalls on calling Julia from Fortran.

The situation:  
Coupled Climate Model (Fortran coupler & Fortran ocean, ice, atmosphere, land models,…) which we’d like to be able to have the coupler call a prototype wave model written in Julia.

Looking at  
[https://docs.julialang.org/en/v1/manual/embedding/#Embedding-Julia](https://docs.julialang.org/en/v1/manual/embedding/#Embedding-Julia)  
it seems possible, wondered if anyone had experience or recommendations for this.

Cheers,  
Helen

---

<div class="post-metadata">

### Author: ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)
#### Post date: [October 31, 2023, 8:06pm UTC](https://fortran-lang.discourse.group/t/fortran-calling-julia/6714/2 "2023-10-31T20:06:20Z")

</div>

Hi @hkershaw, welcome to the forum. Maybe there are others here who have already done it. Otherwise I would start with the link you posted, and call those C functions from Fortran via `iso_c_binding`, link everything together and see.

(In the long run, I would like LFortran and other Fortran compilers to gain support for out of the box support for interoperation with other languages including Python, Julia, JavaScript, WASM, etc., automatically converting arguments, and so on.)

---

<div class="post-metadata">

### Author: ![dubos](https://avatars.discourse-cdn.com/v4/letter/d/f1d935/32.png) [@dubos](https://fortran-lang.discourse.group/u/dubos)
#### Post date: [November 1, 2023, 11:36am UTC](https://fortran-lang.discourse.group/t/fortran-calling-julia/6714/3 "2023-11-01T11:36:45Z")

</div>

You may also have a look there:  
[https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/)

This shows how to create a C-compatible function pointer to a Julia function. This allows the possibly less intrusive pattern (not tried myself):

- turn your Fortran main program into a main subroutine with BIND(C) attribute, accepting arguments of type C\_FUNPTR corresponding to the Julia functions you want to call later
- compile your Fortran code with -fPIC and create a shared library
- write a Julia main script that loads your Julia modules, creates callbacks to Julia functions to be called from Fortran, loads your shared lib and calls your main Fortran subroutine with these callbacks as arguments.

---

<div class="post-metadata">

### Author: ![jcwright](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@jcwright](https://fortran-lang.discourse.group/u/jcwright)
#### Post date: [November 5, 2023, 2:47pm UTC](https://fortran-lang.discourse.group/t/fortran-calling-julia/6714/4 "2023-11-05T14:47:45Z")

</div>

Have you considered compiling the Fortran into a Julia routine with a Julia call back? See f2py in Python. Maybe Julia has copied that too.
