- 论坛徽章:
- 0
|
以Windows平台下使用Visual C++6.0为例
C代码:如FOO.C
#include
/* Define the method table. */
static PyObject *foo_bar(PyObject *self, PyObject *args);
static PyMethodDef FooMethods[] = {{"bar", foo_bar, METH_VARARGS},{NULL, NULL}};
/* Here's the initialization function. We don't need to do anything
for our own needs, but Python needs that method table. */
void initfoo()
{
(void) Py_InitModule("foo", FooMethods);
}
/* Finally, let's do something ... involved ... as an example function. */
static PyObject *foo_bar(PyObject *self, PyObject *args)
{
char *string;
int len;
if (!PyArg_ParseTuple(args, "s", &string))
return NULL;
len = strlen(string);
return Py_BuildValue("i", len);
}
C定义文件:foo.def
EXPORTS
initfoo
编译生成foo.dll
cl -c foo.c
link foo.obj /dll /def:foo.def /OUT:foo.dll
(注:设置环境变量include、lib;或在相应的命令加相应的参数)
在PYTHON中调用:
Import foo
Dir(foo)
…
即可以看到结果:
>>> import foo
>>> dir(foo)
['__doc__', '__file__', '__name__', 'bar']
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8780/showart_179319.html |
|