# Can Fortran subroutines be passed as argument to the function called in ccall

**URL:** <https://discourse.julialang.org/t/can-fortran-subroutines-be-passed-as-argument-to-the-function-called-in-ccall/56426>\
**Category:** New to Julia\
**Tags:** fortran\
**Created:** [March 3, 2021, 5:16pm UTC](https://discourse.julialang.org/t/can-fortran-subroutines-be-passed-as-argument-to-the-function-called-in-ccall/56426 "2021-03-03T17:16:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![amarcotte59](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@amarcotte59](https://discourse.julialang.org/u/amarcotte59)\
**Post date:** [March 3, 2021, 5:16pm UTC](https://discourse.julialang.org/t/can-fortran-subroutines-be-passed-as-argument-to-the-function-called-in-ccall/56426/1 "2021-03-03T17:16:53Z")

</div>

I searched and saw documentation and discussions on passing different types of variables but nothing on passing subroutines or functions as argument in ccall. We are in the process of converting our fortran programs. Most are using library optimisation subroutine which need the objective and gradient fonctions to be passed as arguments.  
Can functions/subroutines be passed as argument to the function called in ccall?  
If so where could I find information or documentation for this; what would be the type to declare in the tuple and how to get a handle on the subroutine to be passed. Would that handle be similar to the function passed as first argument to ccall?

---

<div class="post-metadata">

**Author:** ![helgee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgee/32/2022_2.png) [@helgee](https://discourse.julialang.org/u/helgee)\
**Post date:** [March 3, 2021, 7:17pm UTC](https://discourse.julialang.org/t/can-fortran-subroutines-be-passed-as-argument-to-the-function-called-in-ccall/56426/2 "2021-03-03T19:17:28Z")

</div>

I would recommend writing a thin `iso_c_binding` wrapper in Fortran90+ (here is an example [benchmarks/dopri.f90 at master · OpenAstrodynamics/benchmarks · GitHub](https://github.com/OpenAstrodynamics/benchmarks/blob/master/lib/dopri/dopri.f90)) which makes calling Fortran from Julia much easier.

The type in Fortran would then be `type(c_funptr), intent(in), value :: func` and the corresponding Julia type in the `ccall` should be `Ptr{Cvoid}`.

You can get a handle to the function by doing

```julia
using Libdl

fortran_lib = dlopen("fortran_library.so")
func_ptr = dlsym(fortran_lib, :fortran_func)

```

and use it in a `ccall`

```julia
ccall((:iso_c_fortran_func, fortran_lib), Cvoid, (Ptr{Cvoid},), func_ptr)

```

(Disclaimer: It has been a long time since I last did this and have not had time to try a minimal example.)

---

<div class="post-metadata">

**Author:** ![amarcotte59](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@amarcotte59](https://discourse.julialang.org/u/amarcotte59)\
**Post date:** [March 3, 2021, 7:36pm UTC](https://discourse.julialang.org/t/can-fortran-subroutines-be-passed-as-argument-to-the-function-called-in-ccall/56426/3 "2021-03-03T19:36:17Z")

</div>

Thanks, it is a good starting point. I’ll update this thread after attempting to implement along those lines.

---

<div class="post-metadata">

**Author:** ![amarcotte59](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@amarcotte59](https://discourse.julialang.org/u/amarcotte59)\
**Post date:** [March 26, 2021, 9:23pm UTC](https://discourse.julialang.org/t/can-fortran-subroutines-be-passed-as-argument-to-the-function-called-in-ccall/56426/4 "2021-03-26T21:23:26Z")

</div>

Maybe because I’m using gfortran as the fortran compiler but I didn’t need to use explicitly the `iso_c_binding` wrapper.  
Simply using dlsym to get the pointer and declaring `Ptr{Cvoid}` as type was enough to get it working.  
I had some delay in preparing the fortran code (splitting the main in pieces that could be called by Julia), hence the timing of this response.  
If anyone as any cautions, warnings about skipping the `iso_c_binding` wrapper, please share them in this tread.

---

<div class="post-metadata">

**Author:** ![helgee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgee/32/2022_2.png) [@helgee](https://discourse.julialang.org/u/helgee)\
**Post date:** [March 27, 2021, 9:51am UTC](https://discourse.julialang.org/t/can-fortran-subroutines-be-passed-as-argument-to-the-function-called-in-ccall/56426/5 "2021-03-27T09:51:02Z")

</div>

`iso_c_binding` is not strictly necessary if your code is simple (e.g. no modules) and targets only a single compiler. I still find it useful because it gives a consistent interface.

If you are targeting different compilers however, it becomes a necessity if you do not want to deal with different name mangling conventions and other headaches.

---

<div class="post-metadata">

**Author:** ![amarcotte59](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@amarcotte59](https://discourse.julialang.org/u/amarcotte59)\
**Post date:** [June 30, 2021, 3:45pm UTC](https://discourse.julialang.org/t/can-fortran-subroutines-be-passed-as-argument-to-the-function-called-in-ccall/56426/6 "2021-06-30T15:45:10Z")

</div>

I was able to run a main in Julia calling a fortran library requiring a call back which I provided by wrapping fortran code in a julia function. The memory for the data was allocated on the Fortran side but to adapt to the the library that will be converted to Julia I will need to allocate the data on the Julia side and have the Fortran code (legacy application) use that data.  
The project required other work so the Julia-Fortran handshake part is on pause for now but once the allocation in Julia is completed I plan to document all my findings with examples and publish here.  
I don’t feel I master the subject yet but can probably help those starting on the Fortran-Julia path.
