Fortran + Julia



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?


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. 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.


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.


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 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.


Thanks, that might work.


Fortran supports C function pointers, so you might be able do this using cfunction 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!)


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