我试图从Python脚本访问fortran77公共块中存储的数据。问题是我不知道这些数据存储在哪里。 我正在开发的Python应用程序使用不同的库。这些库包含具有以下指令的函数:
#include <tcsisc_common.inc>
公共块包含:
^{pr2}$在Python库上使用了iPython(ipythi)的示例:
In [1]: import ctypes
In [2]: _libtcsisc= /home/jfreixa/project/bin/libtcsisc.so
In [3]: _tcsisc = ctypes.CDLL(_libtcsisc, ctypes.RTLD_GLOBAL)
问题是我不知道如何得到IDEBUG。我尝试了以下方法,但我只是将tcsd作为一个c_初始化为0。在
In [4]: tcsd = ctypes.c_int.in_dll(_tcsisc, "TCSD_")
In [5]: tcsd
Out[5]: c_long(0)
In [6]: idebug = ctypes.c_int.in_dll(_tcsisc, "IDEBUG_")
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-6-ee5018286275> in <module>()
----> 1 idebug = ctypes.c_int.in_dll(_tcsisc,'IDEBUG_')
ValueError: ld.so.1: python2.7: fatal: IDEBUG_: can't find symbol
有没有办法正确地得到变量?在
根据this page(特别是如何从C访问Fortran公共块)和一些关于如何从Python访问C结构的Q/A page来看,我们似乎可以按如下方式访问公共块(尽管这可能不是很容易移植,请参见下文):
mylib.f90型
编译:
^{pr2}$在测试.py在
测试:
这里,请注意公共块的名称(这里,
mycom
)是小写的,并附加一个下划线(假设为gfortran)。因为这种约定依赖于编译器,所以编写新的Fortran例程来设置/获取公共块中的值(特别是借助iso_c_binding
)并从Python调用这些例程(正如@innoSPG在第一条注释中所建议的那样)可能更健壮/更易于移植。在另一个包含不同类型和数组的示例可能如下所示:
mylib.f90型
在测试.py在
测试:
相关问题 更多 >
编程相关推荐