OS X: python ctypes 全局变量
OS X: python ctypes global variables
我正在尝试为使用 C99 标准编写的项目创建 Python ctypes 绑定。当前的 C 代码使用一些全局变量(例如 b
、c
和 d
)与顶级函数(例如 mod_run
)结合使用。
示例:model.c
#include <mod_def.h>
#include <mod_run.h>
struct_b *b;
int d
int
mod_run(int rec,
double state,
struct_a *a)
{
extern struct_c c;
// code that uses a, b, c, d, rec, state
// and functions defined in mod_def.h and mod_run.h
}
我已经使用 setuptools.extension
模块成功创建了共享对象(例如 model.so
),但无法使用 ctypes.cdll.LoadLibrary
加载该对象。
In [1]: from ctypes import *
In [2]: cdll.LoadLibrary('model.so')
OSError: dlopen(model.so, 6): Symbol not found: _X
Referenced from: model.so
Expected in: dynamic lookup
其中 X
是在 mod_def.h
中声明的全局变量:
extern size_t X;
最后,我的问题。我是否需要围绕 mod_run
创建一个包装器,以允许我导出示例代码中定义的每个全局变量?或者是否有可能以某种方式加载共享对象,而无需定义 X
?
我查看了这些相关主题,但没有找到解决我问题的方法:
- Mapping a global variable from a shared library with ctypes
- Python ctypes in_dll string assignment
更新 (05/20/2015):
我如何构建共享对象:
setup(name='model',
...
ext_modules=[Extension(
'model',
sources=sources, # list of c sources using absolute paths
include_dirs=includes, # list of include directories using absolute paths
extra_compile_args=['-std=c99'],
# extra_link_args=['-lmodule_with_globals'] # does not build with this option: ld: library not found for -lmodule_with_globals
)])
使用 OSX 10.10.3 和 Python 3.4.3 :: Anaconda 2.2.0 (x86_64)。
问题是变量 X
是在 header 文件中定义的,但从未在代码中的任何地方分配。
外部 size_t X;
为了解决这个问题,我创建了一个名为 globals.c
的包装文件,并在其中简单地输入:
// globals.c
#include <mod_def.h>
#include <mod_run.h>
size_t X;
// allocation of other global variables
就这样,
from ctypes import *
# load the library (this was failing before)
lib = cdll.LoadLibrary('model.so')
# create a python variable that maps to the global X
X = c_sizet.in_dll(lib, 'X')
# assign a value to the global X
X.value = 2
分配可能在现有源文件之一的 header 中,但就我而言,这就是我们现在希望模块工作的方式。
我正在尝试为使用 C99 标准编写的项目创建 Python ctypes 绑定。当前的 C 代码使用一些全局变量(例如 b
、c
和 d
)与顶级函数(例如 mod_run
)结合使用。
示例:model.c
#include <mod_def.h>
#include <mod_run.h>
struct_b *b;
int d
int
mod_run(int rec,
double state,
struct_a *a)
{
extern struct_c c;
// code that uses a, b, c, d, rec, state
// and functions defined in mod_def.h and mod_run.h
}
我已经使用 setuptools.extension
模块成功创建了共享对象(例如 model.so
),但无法使用 ctypes.cdll.LoadLibrary
加载该对象。
In [1]: from ctypes import *
In [2]: cdll.LoadLibrary('model.so')
OSError: dlopen(model.so, 6): Symbol not found: _X
Referenced from: model.so
Expected in: dynamic lookup
其中 X
是在 mod_def.h
中声明的全局变量:
extern size_t X;
最后,我的问题。我是否需要围绕 mod_run
创建一个包装器,以允许我导出示例代码中定义的每个全局变量?或者是否有可能以某种方式加载共享对象,而无需定义 X
?
我查看了这些相关主题,但没有找到解决我问题的方法:
- Mapping a global variable from a shared library with ctypes
- Python ctypes in_dll string assignment
更新 (05/20/2015):
我如何构建共享对象:
setup(name='model',
...
ext_modules=[Extension(
'model',
sources=sources, # list of c sources using absolute paths
include_dirs=includes, # list of include directories using absolute paths
extra_compile_args=['-std=c99'],
# extra_link_args=['-lmodule_with_globals'] # does not build with this option: ld: library not found for -lmodule_with_globals
)])
使用 OSX 10.10.3 和 Python 3.4.3 :: Anaconda 2.2.0 (x86_64)。
问题是变量 X
是在 header 文件中定义的,但从未在代码中的任何地方分配。
外部 size_t X;
为了解决这个问题,我创建了一个名为 globals.c
的包装文件,并在其中简单地输入:
// globals.c
#include <mod_def.h>
#include <mod_run.h>
size_t X;
// allocation of other global variables
就这样,
from ctypes import *
# load the library (this was failing before)
lib = cdll.LoadLibrary('model.so')
# create a python variable that maps to the global X
X = c_sizet.in_dll(lib, 'X')
# assign a value to the global X
X.value = 2
分配可能在现有源文件之一的 header 中,但就我而言,这就是我们现在希望模块工作的方式。