python调用c的方法

python调用c的方法研究了三天,今天看了一篇博客
原来这么简单,可能是最简单的方法了吧。下面整理一下思路

实验平台:
window xp系统,编译环境是vs2005

实验步骤:
步骤1:
在vs2005下编写c模块
代码如下

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include "Python.h"

_declspec(dllexport) int fac(int n)
{
printf("fac fn run.\n");
if(n < 2)
return 1;
return n * fac(n - 1);
}

步骤2:
编译生成dll库

步骤3:
在python中加载动态库并测试。
代码如下
def test_py_call_clib():
    from ctypes import *
    import os
    test_other = cdll.LoadLibrary(os.getcwd() + '/test_other.dll')
    print "load library success."
    print test_other.fac(4)

if __name__ == "__main__":
test_py_call_clib()

注意加载动态库的目录


参考
http://coolshell.cn/articles/671.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值