如何用c语言写python库_用C语音编写python的扩展模块,也就是python调c库

1.用C语言扩展Python的功能:

http://www.ibm.com/developerworks/cn/linux/l-pythc/

2.用C语言编写Python扩展模块:

http://hi.baidu.com/jinmu190/blog/item/c5475846eee39c056a63e5f1.html

http://blog.csdn.net/dengxu11/article/details/7393395

http://www.cnblogs.com/phinecos/archive/2010/05/23/1742344.html

我们知道python是一种很好的胶水语言,现在有个问题,如何让python 调用到c程序,

假如c程序已经是成熟的code.

方式有3种:

1.直接调用动态库

2.通过SWIG接口

前面2中方式参考: http://www.2cto.com/kf/201009/74906.html

3.为调用者python 写一个c的扩展包

需求:有一个POC的项目,需要在openstack的 源码里面加入我们自己的控制逻辑.openstack是

python 写的,我们自己的控制逻辑是个成熟的code,(大量的so), 我们需要在python code中调用

c code. 我们采用 方式3 为python写一个 扩展包,然后在python中调用 c code

例子:为say.c 编写一个 符合python 标准的扩展包

a. say.h 已经存在,里面有2个函数定义 sayhello, saybye

b. say.c 已经存在,里面实现了 say.h 中定义的方法

c. sayModule.c 是为 python 调用者写的 一个扩展包,最终会使用 distutils 编译成say.so

d. setUp.py,将c code 打包成 so :

[root@egovmo03 C]# python setup.py install

running install

running build

running build_ext

building 'say' extension

creating build

creating build/temp.linux-x86_64-2.4

gcc -pthread -fno-strict-aliasing -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fPIC -I/usr/include/python2.4 -c sayModule.c -o build/temp.linux-x86_64-2.4/sayModule.o

gcc -pthread -fno-strict-aliasing -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fPIC -I/usr/include/python2.4 -c say.c -o build/temp.linux-x86_64-2.4/say.o

creating build/lib.linux-x86_64-2.4

gcc -pthread -shared build/temp.linux-x86_64-2.4/sayModule.o build/temp.linux-x86_64-2.4/say.o -o build/lib.linux-x86_64-2.4/say.so

running install_lib

copying build/lib.linux-x86_64-2.4/say.so -> /usr/lib64/python2.4/site-packages

[root@egovmo03 C]#

测试:

[root@egovmo03 C]# python

Python 2.4.3 (#1, Jun 11 2009, 14:09:37)

[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import say

>>> dir(say)

['__doc__', '__file__', '__name__', 'bye', 'error', 'hello', 'say']   ->>> say 模块多了 hello 和 bye方法

>>> aa='xpxp'

>>> say.hello(aa);

hello, xpxp

>>> say.bye(aa);

bye, xpxp

>>>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言代码转换为Python语言是不可行的,因为两种语言的语法和特性都有很大的差异。但是,可以使用C语言扩展Python的功能。下面是一个简单的示例: 在C语言编写一个函数,用于计算两个整数的和: ```c int add(int a, int b) { return a + b; } ``` 然后,使用Python的C扩展接口将该函数添加到Python中: ```c #include <Python.h> int add(int a, int b) { return a + b; } static PyObject* add_wrapper(PyObject* self, PyObject* args) { int a, b; if (!PyArg_ParseTuple(args, "ii", &a, &b)) return NULL; return Py_BuildValue("i", add(a, b)); } static PyMethodDef module_methods[] = { {"add", add_wrapper, METH_VARARGS, "Add two integers."}, {NULL, NULL, 0, NULL} }; static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "mymodule", NULL, -1, module_methods }; PyMODINIT_FUNC PyInit_mymodule(void) { return PyModule_Create(&moduledef); } ``` 这个例子中,我们定义了一个名为“add”的函数,在Python中以模块的形式使用。我们使用PyArg_ParseTuple函数将传递给Python函数的参数解析为C变量,并使用Py_BuildValue函数将C变量转换为Python对象。 最后,我们使用PyModule_Create函数创建一个名为“mymodule”的模块,该模块包含我们刚刚定义的函数。可以将该模块编译成共享,并在Python中使用它。 使用方法如下: ```python import mymodule print(mymodule.add(1, 2)) # 输出3 ``` 这就是用C语言扩展Python的简单示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值