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

怎甘沉沦 提交于 2019-11-28 07:05:04

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!