cython和SWIG

1 cython

C语言可以调用python

Created with Raphaël 2.2.0 编写xx.pyx文件 cython编译生成xx.c和xx.h 在c代码中调用python函数

编写c.pyx文件

cdef public c_func(const char *str):
    print(str)

cython编译
编译:cython c.pyx
生成文件c.c和c.h
c.h

#if PY_MAJOR_VERSION < 3
PyMODINIT_FUNC initc(void);
#else
PyMODINIT_FUNC PyInit_c(void);
#endif

#endif /* !__PYX_HAVE__c */

c调用python函数
c_py.c

#include<Python.h>
#include"c.h"

int main(int argc,char *argv[])
{
		Py_Initialize();
		initc();
		c_func("hello python\n");
		Py_Finalize();
}

编译:gcc c_py.c c.c -o c_py -I/usr/include/python2.7/ -lpython2.7
执行输出: hello python

2 SWIG

python 调用C

Created with Raphaël 2.2.0 编写xx.i文件 swig进行编译生成xx_wrap.c和xx.py 编译成python扩展 调用执行

编写.i文件

/* swig.i*/
%module swig
%{
void python()
{
   printf("hello python\n");     
}
%}
void python();

swig进行编译
swig -python swig.i
生成swig_wrap.c和swig.py
编译成python扩展
gcc -fPIC -shared swig_wrap.c -o _swig.so -I/usr/include/python2.7/ -lpython2.7
调用执行
import swig即可以使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值