Pull requests

#5 Merged
jas43 jas43
haraldkl haraldkl

Allow aotus to be used in a shared library

  1. jas43

These patches make it possible to create a shared library out of Fortran procedures which can be called directly from lua and also use the AOTUS wrappers to modify the lua stack. So, hopefully it will be quite easy to extend an existing program using AOTUS to also be compiled into a library which can then nicely used in scripts rather than having to write scripts to create input files.

As an example:

module libtest

use, intrinsic :: iso_c_binding
use flu_binding

implicit none


    function luaopen_libtest(lua_state) result(val) bind(c)

        type(c_ptr), value :: lua_state
        integer(c_int) :: val
        type(flu_state) :: fL

        fL = flu_copyptr(lua_state)

        call flu_register(fL, "hello_f90", hello_f90)

        val = 0

    end function luaopen_libtest

    function hello_f90(lua_state) result(val) bind(c)

        use aotus_module

        type(c_ptr), value :: lua_state
        integer(c_int) :: val
        integer :: x, err
        type(flu_state) :: fL

        fL = flu_copyptr(lua_state)
        write (6,*) "HELLO FROM FORTRAN! :-)"
        call flu_pushinteger(fL, 102)
        call flu_setglobal(fL, "af90")
        val = 0

    end function hello_f90

end module libtest

can be compiled into a shared library with aotus (no need to link to lua and I used Make to compile as I couldn't easily see how to modify the waf script to do this). hello_f90 can then be used from a lua script:

$ lua -e "require('libtest'); hello_f90(); print(af90)" HELLO FROM FORTRAN! :-) 102

Comments (2)

  1. Harald Klimach repo owner

    Thanks a lot!

    In the wscript you could put a new target with a shlib feature, something like this:

        features = 'fc fcshlib',
        source = 'libtest.f90',
        use = ['flu', 'aotus'],
        target = 'libtest')