我正在尝试使用f2py为我的Fortran代码创建一个使用PETSc和SLEPc的python模块(这个问题与this post密切相关,但是那里的讨论无法解决我的问题 - 见本帖底部的评论)。代码由许多文件和子例程组成,但我只需要访问python中最外层的main / driver函数。我采取的第一步是使用SLEPc提供的makefile模板生成所有目标文件(最终链接失败,因为外部函数现在是一个子程序而不是主程序,但我现在不担心这个因为我只是在目标文件之后。)
从那以后,我尝试了两种方法。第一种是简单地按如下方式呼叫f2py
:
f2py -c -m modname outer_driver.f90 file1.o file2.o file3.o...
这似乎有效,但在尝试导入到python时,无法找到SLEPc(以及我认为,PETSc)对象/函数:
>>> import mod_name
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(./mod_name.so, 2): Symbol not found: _epscreate_
Referenced from: ./mod_name.so
Expected in: flat namespace
in ./mod_name.so
第二种方法假设我还需要链接SLEPc / PETSc库。因此,我在makefile中添加了一行:
modname.so: outer_driver.f90
f2py -c -m modname outer_driver.f90 file1.o file2.o file3.o ${SLEPC_EPS_LIB}
运行&#34; make modname.so,&#34; f2py
无法完全运行。以下是该输出:
running build
running config_cc
unifing config_cc, config, build_clib, build_ext, build commands --compiler options
running config_fc
unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
running build_src
build_src
building extension "modname" sources
f2py options: []
f2py:> /tmp/tmpIH70ZJ/src.macosx-10.10-x86_64-2.7/modnamemodule.c
creating /tmp/tmpIH70ZJ/src.macosx-10.10-x86_64-2.7
Reading fortran codes...
Reading file 'outer_driver.f90' (format:free)
Post-processing...
Block: modname
Block: outer_driver
Post-processing (stage 2)...
Building modules...
Building module "modname"...
Constructing wrapper function "outer_driver"...
outer_driver()
Wrote C/API module "modname" to file "/tmp/tmpIH70ZJ/src.macosx-10.10-x86_64-2.7/modnamemodule.c"
adding '/tmp/tmpIH70ZJ/src.macosx-10.10-x86_64-2.7/fortranobject.c' to sources.
adding '/tmp/tmpIH70ZJ/src.macosx-10.10-x86_64-2.7' to include_dirs.
copying /usr/local/lib/python2.7/site-packages/numpy/f2py/src/fortranobject.c -> /tmp/tmpIH70ZJ/src.macosx-10.10-x86_64-2.7
copying /usr/local/lib/python2.7/site-packages/numpy/f2py/src/fortranobject.h -> /tmp/tmpIH70ZJ/src.macosx-10.10-x86_64-2.7
build_src: building npy-pkg config files
running build_ext
customize UnixCCompiler
customize UnixCCompiler using build_ext
customize Gnu95FCompiler
Found executable /usr/local/bin/gfortran
customize Gnu95FCompiler
customize Gnu95FCompiler using build_ext
building 'modname' extension
compiling C sources
C compiler: clang -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
error: unknown file type '' (from '-Wl,-rpath,/usr/local/Cellar/slepc/3.7.3_4/real/lib')
make: *** [modname.so] Error 1
Using f2py on a fortran code linked to PETSc帖子的OP声明他可以绕过使用-Wl,-rpath
标志,这似乎也是一个问题,方法是将LD_LIBRARY_PATH
环境变量设置为PETSc库。但是,如果我做同样的事情(设置为SLEPc库,我相信也暗示着PETSc库),对我来说这对任何一种方法都没有帮助。任何人都可以提出我可能会使用的方法吗?
更新:请求了关于-Wl,rpath
错误及其来源的更多信息。据我所知,这些标志来自使用SLEPc / PETSc链接。所以,在我的makefile中,它们来自${SLEPC_EPS_LIB}
变量。解释时,完整的命令非常长,但无论如何我都会包含它,以防可以从那里开采。请记住,我在上面的示例中没有使用实际的文件名 - 为了清晰起见,我简化了一下。文件名将在下面不同。
f2py -c -m iga_blade_py run_analysis.f90 modules.o forces.o param.o cname.o curve_input_nrb_mc.o shell_input_nrb_mp.o shell_reduce_node.o shell_input_nrb.o material_input.o material_calculate.o shell_laminate2.o iosol.o shell_gp_info。 øshell_profile_vector.o shell_genIEN_INN.o shell_genSparStruc.o shell_genGP_GW.o shell_eval_SHAPE.o shell_dersbasisfuns.o shell_e3LRhs_KLShell.o shell_e3LRhs_KLShell_pen_disp.o shell_e3LRhs_KLShell_pen_rot.o shell_KL_geo.o cross_prod.o shell_BCLhs_3D.o shell_FillSparseMat_3D.o shell_SparseMatLoc_3D.o shell_SparseCG.o shell_skyline.o shell_SparseProd的.o shell_IntElmAss_post2.o shell_e3LRhs_KLShell_post.o curve_element_project.o shell_mnrb_find_search_locs.o shell_nrb_find_point.o patch_point_proj.o petsc_genSparStruc.o viz_read_mesh_points.o driver.o input_shell.o solflow.o shell_IntElmAss.o petsc_fillMat.o shell_IntElmAss_pen.o petsc_prepSolve.o shell_allocate_LRhs.o petsc_rank.o solflow_lin_buck.o solflow_vibr.o shell_IntElmAss_mass.o -Wl,-rpath,/ usr / l ocal / Cellar / slepc / 3.7.3_4 / real / lib -L / usr / local / Cellar / slepc / 3.7.3_4 / real / lib -lslepc -Wl,-rpath,/ usr / local / opt / arpack / lib - L / usr / local / opt / arpack / lib -lparpack -larpack -Wl,-rpath,/ usr / local / Cellar / petsc / 3.7.5 / real / lib -L / usr / local / Cellar / petsc / 3.7。 5 / real / lib -lpetsc -L / usr / local / opt / superlu_dist / lib -lsuperlu_dist -Wl,-rpath,/ usr / local / opt / mumps / libexec / lib -L / usr / local / opt / mumps / libexec / lib -lcmumps -ldmumps -lsmumps -lzmumps -lmumps_common -lpord -Wl,-rpath,/ usr / local / opt / parmetis / lib -L / usr / local / opt / parmetis / lib -lparmetis -Wl,-rpath ,/ usr / local / opt / metis / lib -L / usr / local / opt / metis / lib -lmetis -Wl,-rpath,/ usr / local / opt / hypre / lib -L / usr / local / opt / hypre / lib -lHYPRE -Wl,-rpath,/ usr / local / opt / libevent / lib -L / usr / local / opt / libevent / lib -Wl,-rpath,/ usr / local / Cellar / open-mpi / 2.0.2 / lib -L / usr / local / Cellar / open-mpi / 2.0.2 / lib -Wl,-rpath,/ usr / local / lib -L / usr / local / lib -Wl,-rpath,/系统/库/框架/ OpenGL.framework / Versions / Current / Libraries -L / System / Library / Frameworks / OpenGL。 framework / Versions / Current / Libraries -Wl,-rpath,/ Applications / Xcode.app / Contents / Developer / Toolchains / XcodeDefault.xctoolchain / usr / lib / clang / 8.0.0 / lib / darwin -L / Applications / Xcode。 app / Contents / Developer / Toolchains / XcodeDefault.xctoolchain / usr / lib / clang / 8.0.0 / lib / darwin -lclang_rt.osx -lc ++ -Wl,-rpath,/ Applications / Xcode.app / Contents / Developer / Toolchains / XcodeDefault.xctoolchain / usr / bin /../ lib / clang / 8.0.0 / lib / darwin -L / Applications / Xcode.app / Contents / Developer / Toolchains / XcodeDefault.xctoolchain / usr / bin /../ lib / clang / 8.0.0 / lib / darwin -lclang_rt.osx -Wl,-rpath,/ usr / local / opt / scalapack / lib -L / usr / local / opt / scalapack / lib -lscalapack -Wl,-rpath,/ usr / local / opt / suite-sparse / lib -L / usr / local / opt / suite-sparse / lib -lumfpack -lklu -lcholmod -lbtf -lccolamd -lcolamd -lcamd -lamd -lsuitesparseconfig -Wl,-rpath,/ usr / local / opt / sundials / lib -L / usr / local / opt / sundials / lib -lsundials_cvode -lsundials_nvecserial -lsundials_nvecparallel -llapack -lblas -Wl,-rpath,/ usr / local / opt / netcdf / lib -L / USR /本地的/ opt / N etcdf / lib -lnetcdf -Wl,-rpath,/ usr / local / opt / hdf5 / lib -L / usr / local / opt / hdf5 / lib -lhdf5_hl -lhdf5 -Wl,-rpath,/ usr / local / opt / hwloc / lib -L / usr / local / opt / hwloc / lib -lhwloc -Wl,-rpath,/ usr / local / opt / fftw / lib -L / usr / local / opt / fftw / lib -lfftw3_mpi -lfftw3 - lmpi_usempif08 -lmpi_usempi_ignore_tkr -lmpi_mpifh -lgfortran -Wl,-rpath,/ usr / local / Cellar / gcc / 6.3.0_1 / lib / gcc / 6 / gcc / x86_64-apple-darwin15.6.0 / 6.3.0 -L / usr / local / Cellar / gcc / 6.3.0_1 / lib / gcc / 6 / gcc / x86_64-apple-darwin15.6.0 / 6.3.0 -Wl,-rpath,/ usr / local / Cellar / gcc / 6.3.0_1 / lib / gcc / 6 -L / usr / local / Cellar / gcc / 6.3.0_1 / lib / gcc / 6 -lgfortran -lgcc_ext.10.5 -lquadmath -lm -lclang_rt.osx -lc ++ -lclang_rt.osx -Wl,-rpath,/ usr / local / opt / libevent / lib -L / usr / local / opt / libevent / lib -Wl,-rpath,/ usr / local / Cellar / open-mpi / 2.0.2 / lib -L / usr / local / Cellar / open-mpi / 2.0.2 / lib -Wl,-rpath,/ usr / local / lib -L / usr / local / lib -Wl,-rpath,/ System / Library / Frameworks / OpenGL.framework / Versions /当前/图书馆-L /系统/图书馆/框架/ OpenGL.framework / Versions / Current / Libraries -ldl -lmpi -lSystem -Wl,-rpath,/ Applications / Xcode.app / Contents / Developer / Toolchains / XcodeDefault.xctoolchain / usr / bin /../ lib / clang / 8.0.0 / lib / darwin -L / Applications / Xcode.app / Contents / Developer / Toolchains / XcodeDefault.xctoolchain / usr / bin /../ lib / clang / 8.0.0 / lib / darwin -lclang_rt.osx -ldl
已解决:我最终能够找到几种不同的方法来解决这个问题。最简单的方法与我描述的第二种(基于makefile的)方法基本相同,但我手动删除-Wl,rpath
变量中的每个SLEPC_EPS_LIB
命令。另外,我通过PETSc邮件列表了解到,只能使用f2py来生成必需的.c,.f90和.pyc文件。然后,您可以自己编译,链接到必需的库。显然这涉及更多,但它也是一种选择。