Including a compiled module in module that is wrapped with f2py (Minimum working example)?

后端 未结 1 653
一生所求
一生所求 2020-12-08 17:42

I have tried, but am failing, to get a minimum working example. As I do not need to expose much of my fortran code to python, I don\'t need f2py to wrap large parts of it. A

相关标签:
1条回答
  • 2020-12-08 18:35

    Your command:

    gfortran -c -fPIC libtest.f90
    

    produces an object file with position independent code. This is a pre-requisite of a shared library, not a shared library.

    If you want to use the object as is, you can modify your f2py invocation:

    f2py -c --fcompiler=gfortran -I. libtest.o -m Main main.f90
    

    This will link the object file and produce the file Main.cpython-33.so (the python version number may differ for you) and you can then import main in your python code.


    If you would rather actually produce a shared object, you need to compile to a shared library. One way to do this is:

    gfortran -shared -O2 -o libtest.so -fPIC libtest.f90
    

    This produces libtest.so and now your original f2py command will work with one small change:

    f2py -c --fcompiler=gfortran -L. -I. -ltest -m Main main.f90
    

    The small change I am referring to is changing -llibtest to -ltest, as the -l option will add lib to the front of the library and .so to the end, e.g. -ltest will look for libtest.so. This produces Main.cpython-33.so with a dynamic link dependency to libtest.so, so you will need to distribute both shared libraries in order to use the python module.

    0 讨论(0)
提交回复
热议问题