码迷,mamicode.com
首页 > 编程语言 > 详细

python调用c代码2

时间:2014-06-18 15:45:00      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:class   blog   代码   name   python   type   

1、生成动态链接库

[root@typhoeus79 c]# more head.c 
#include <stdio.h>
#include <stdlib.h>

typedef struct _point{
    int x;
    int y;
}Point;

Point * InitPoint(int x,int y)
{
    Point *p = (Point *)malloc(sizeof(Point));
    p->x = x;
    p->y = y;

    return p;
}

Point * Incre(Point * p)
{
    p->x = p->x + 1;
    p->y = p->y + 1;

    return p;
}
gcc -fPIC -shared -o libhead.so ./head.c 

python调用

[root@typhoeus79 ice_test_m c]# more test.py 
#!/usr/bin/env python2.7
#-*- coding:utf-8 -*-

from ctypes import *

class Point(Structure):
    _fields_ = [("x",c_int),("y",c_int)]

if __name__ == ‘__main__‘:
    ddl = CDLL("./libhead.so")

    ddl.InitPoint.restype = POINTER(Point)

    p = ddl.InitPoint(5,6);


    print p.contents.x
    print p.contents.y
[root@typhoeus79 ice_test_m c]# ./test.py 
5
6

python调用c代码2,布布扣,bubuko.com

python调用c代码2

标签:class   blog   代码   name   python   type   

原文地址:http://www.cnblogs.com/gsblog/p/3791194.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!