Where can I find good examples of integrating (modern) Fortran code with Julia?
I am using the GNU gfortran compiler (on Cygwin) for my own module. A good example will hopefully start from the compilation stage, address mangled names and call the subroutine from Julia via ccall. Most examples I've seen skip the first two stages. On the SO post, I refer to Modern Fortran explicitly because what I've seen so far tends to be for legacy code -- think punchcard-style fixed-width formatting Fortran (that goes for GLMNet, which was allegedly written in 2008 but adheres to those conventions).
So imagine that I have the following module in Fortran90 file named 'f90tojl.f90':
module m
contains
integer function five()
five = 5
end function five
end module m
This example is from here. I compile it with gfortan as follows to create a shared library:
gfortran -shared -O2 f90tojl.f90 -o -fPIC f90tojl.so
And my, admittedly shaky, understanding from reading the julia docs suggest that I should be able to call the function five like so:
ccall( (:__m_MOD_five, "f90tojl"), Int, () )
It didn't work for me. I get ''error compiling anonymous: could not load module f90tojl... ". Anyone cares to enlighten me? I got the sneaky sense I'm doing something silly....
Thanks in advance,
V.
LoadError: ccall: could not find function m_MP_five in library ifortlib.dll
So my guess is that I am not using the correct name mangling. I couldn't find anything online so I tried to view the function name via the object manager in visual studio and an external dll viewer program.
In visual studio I got a meaningless error the external viewer just didn't do anything (although it worked for other dll files). When I type
julia: Libdl.dlopen("ifortlib.dll")
Ptr{Void} @0x000000002a9d8fa0
fwiw. At this point I got so pissed that I decided to install the gfortran compiler and just follow this thread step by step. So in the cmd window, I type:
gfortran -shared -O2 f90tojl.f90 -fPIC -o gfortlib.dll
(I get a warning that -fPIC is ignored, as written previously in this thread). I use the dll viewer to determine the name of the function, its __m_MOD_five indeed. Then
julia: ccall( (:__m_MOD_five, "gfortlib.dll"), Int, () )
LoadError: error compiling anonymous: could not load library "gfortlib.dll" The specified module could not be found.
And
julia: Libdl.dlopen("gfortlib.dll")
LoadError: could not load library "gfortlib.dll" The specified module could not be found.
And I have no clue what to do now.