# Fortran modules and Julia modules

**URL:** <https://discourse.julialang.org/t/fortran-modules-and-julia-modules/45053>\
**Category:** General Usage\
**Tags:** question, fortran\
**Created:** [August 16, 2020, 4:18pm UTC](https://discourse.julialang.org/t/fortran-modules-and-julia-modules/45053 "2020-08-16T16:18:26Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adelson\_Oliveira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adelson_oliveira/32/8213_2.png) [@Adelson\_Oliveira](https://discourse.julialang.org/u/Adelson_Oliveira)\
**Post date:** [August 16, 2020, 4:18pm UTC](https://discourse.julialang.org/t/fortran-modules-and-julia-modules/45053/1 "2020-08-16T16:18:26Z")

</div>

Hi,

Is already possible to plainly use FORTRAN modules in Julia? If not, are there plans to allow for Julia using FORTRAN modules ( .o and .mod)?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [August 16, 2020, 4:26pm UTC](https://discourse.julialang.org/t/fortran-modules-and-julia-modules/45053/2 "2020-08-16T16:26:04Z")

</div>

It is possible to call fortran from julia, but I don’t know if it matches your definition of “plainly”. The manual has a section on C and fortran calls. [Calling C and Fortran Code · The Julia Language](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#Fortran-Wrapper-Example-1)

---

<div class="post-metadata">

**Author:** ![Adelson\_Oliveira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adelson_oliveira/32/8213_2.png) [@Adelson\_Oliveira](https://discourse.julialang.org/u/Adelson_Oliveira)\
**Post date:** [August 16, 2020, 5:13pm UTC](https://discourse.julialang.org/t/fortran-modules-and-julia-modules/45053/3 "2020-08-16T17:13:54Z")

</div>

First, thanks for the reply!

With plainly I meant something like

julia\> using fortran\_module

or

julia\> import fortran\_module

that could get around known problems with assumed shape and some derived type (class) in subroutines and functions defined in FORTRAN modules…  
I know .mod are compiler dependent but I also know there has been some improvements in C interoperability in FORTRAN 2008 and later so I wonder if it could be a greater compatibility between Julia modules and FORTRAN modules …

Thanks again

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 16, 2020, 5:38pm UTC](https://discourse.julialang.org/t/fortran-modules-and-julia-modules/45053/4 "2020-08-16T17:38:17Z")

</div>

> [@Adelson\_Oliveira](#):
>
> julia\> using fortran\_module
> 
> or
> 
> julia\> import fortran\_module

this was never a thing with any non-Julia module,  
python:

```julia
using PyCall
const np = pyimport("numpy")

```

C++:

```julia
@ccall
or
using Cxx
@cxx

```

R:

```julia
using RCall
@rget

```

---

<div class="post-metadata">

**Author:** ![mkretlow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkretlow/32/16295_2.png) [@mkretlow](https://discourse.julialang.org/u/mkretlow)\
**Post date:** [August 16, 2020, 7:23pm UTC](https://discourse.julialang.org/t/fortran-modules-and-julia-modules/45053/5 "2020-08-16T19:23:58Z")

</div>

No, that is not possible. .mod files do not contain binary code, AFAIK they are somehow like interface files. They contain specifications of such entities as data objects, parameters, structures, procedures, and operators. These pre-compiled specifications and definitions can be used by one or more program units.

You have to compile your Fortran code with a Fortran compiler (don’t forget to compile to position independent code, using GNU Fortran turn on -fPIC option) and to use the ccall() function in Julia. Remember that you have call by reference in Fortran (i.e. use for example Ref{Float64} in your ccall argument list).

---

<div class="post-metadata">

**Author:** ![Huite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huite/32/14034_2.png) [@Huite](https://discourse.julialang.org/u/Huite)\
**Post date:** [August 16, 2020, 8:13pm UTC](https://discourse.julialang.org/t/fortran-modules-and-julia-modules/45053/6 "2020-08-16T20:13:20Z")

</div>

As the other comments say, it’s not quite that straightforward: adding support like that would basically require turning the Julia runtime into a Fortran compiler.

The C interoperability basically comes down to presenting the Fortran code as a C program (essentially, every Fortran compiler is also a C compiler), which is why Julia uses `ccall` to interface with Fortran as well as C.

This stackoverflow example gives a good minimal example:  
[https://stackoverflow.com/questions/27498755/integrating-fortran-code-in-julia](https://stackoverflow.com/questions/27498755/integrating-fortran-code-in-julia)

```fortran
module simpleModule

contains
function foo(x)
  integer :: foo, x
  foo = x * 2
end function foo

end module simplemodule

```

```julia
a = Int32[3]
ccall((:__simplemodule_MOD_foo, "./simplemodule.so"), Int32, (Ptr{Int32},), a)

```

In case of gfortran, it prepends the module name. The intel compilers behave differently, and they also won’t export all routines by default, you need to include a directive.  
To avoid name mangling, it’s best to use the iso\_c\_binding to fully control the exported names.

Depending on the complexity of your program, you can either write the bindings yourself, or write a script which generates them (which I guess is what for example `f2py` does for Python) – at any rate, you’ll still need a fortran compiler to build a shared library.

I’ve played around with this a little while ago, you can find a non-trivial example here for both ifort and gfortran (on Windows, but it easily translates to \*nix):  
[https://github.com/Huite/clipping-benchmarks](https://github.com/Huite/clipping-benchmarks)

See notebook 5 for the Julia example, the fortran code is in the src dir.

---

<div class="post-metadata">

**Author:** ![Adelson\_Oliveira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adelson_oliveira/32/8213_2.png) [@Adelson\_Oliveira](https://discourse.julialang.org/u/Adelson_Oliveira)\
**Post date:** [August 17, 2020, 12:37am UTC](https://discourse.julialang.org/t/fortran-modules-and-julia-modules/45053/7 "2020-08-17T00:37:20Z")

</div>

Thanks everybody for the answers

---

<div class="post-metadata">

**Author:** ![Juliet](https://avatars.discourse-cdn.com/v4/letter/j/97f17d/32.png) [@Juliet](https://discourse.julialang.org/u/Juliet)\
**Post date:** [August 17, 2020, 3:46pm UTC](https://discourse.julialang.org/t/fortran-modules-and-julia-modules/45053/8 "2020-08-17T15:46:54Z")

</div>

There is no need to follow the name-mangling conventions of individual Fortran compilers. The `iso_c_binding` intrinsic module and `bind()` attribute of Fortran 2003 together provide a portable interface to all other languages that can interoperate with C, including Julia. The name-mangling conventions can be overridden by adding the `bind()` attribute to an interoperable procedure.

---

<div class="post-metadata">

**Author:** ![Huite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huite/32/14034_2.png) [@Huite](https://discourse.julialang.org/u/Huite)\
**Post date:** [August 18, 2020, 3:13pm UTC](https://discourse.julialang.org/t/fortran-modules-and-julia-modules/45053/9 "2020-08-18T15:13:46Z")

</div>

Indeed – I mentioned this?

> [@Huite](#):
>
> To avoid name mangling, it’s best to use the iso\_c\_binding to fully control the exported names.

The intel compiler does need the directive:

```julia
!DEC$ ATTRIBUTES DLLEXPORT :: {name_of_routine}

```
