pythonfortrangfortranmeson-buildf2py

Python 3.12 f2py linking against libraries not working properly


With Python 3.11 and the distutils backend I was able to easily compile one interface file and link to an archive with various other precompiled f90 files. Now with meson it seems like this is not possible anymore.

So I have two subroutines:

subroutine1.f90

subroutine subroutine1
    implicit none
    call subroutine2
    print *, "Inside subroutine1"
end subroutine subroutine1

and

subroutine2.f90

subroutine subroutine2
    implicit none
    print *, "Inside subroutine2"
end subroutine subroutine2

I compile both files with gfortran -c subroutine1.f90 subroutine2.f90 and put them into an archive by the command ar rcs libmylibrary.a subroutine1.o subroutine2.o. Lastly I have a main file

main.f90

subroutine main
    implicit none
    call subroutine1()
end subroutine main

and when I use f2py -c main.f90 -m inter -L. -lmylibrary.a I get the interface file. Trying to import the interface in my python file as import inter I get the error: ImportError: /home/XYZ/f2pytest/inter.cpython-312-x86_64-linux-gnu.so: undefined symbol: subroutine1_ and for the life of me I can't figure out why this is not working anymore. When I use the python3.11 version of f2py it compiles no problem.

Any help is greatly appreciated!


Solution

  • You need:

    1. Use -L"$(pwd)" instead of -L., because meson builds the module in a temporary directory, not in the current one;
    2. Use -lmylibrary instead -lmylibrary.a, probably yours mistake.

    For macOS:

    $ python3.12 -m numpy.f2py -c main.f90 -m inter -L"$(pwd)" -lmylibrary
    ...
    ninja: Entering directory `/private/var/folders/wy/z0gbkfgs7mv24ryqkdm90rm40009rh/T/tmpaj6e0z70/bbdir'
    ...
    $ python3.12 -c "import inter; print(inter.__doc__); inter.main()"
    This module 'inter' is auto-generated with f2py (version:1.26.4).
    Functions:
        main()
    .
     Inside subroutine2
     Inside subroutine1