/* func.c */int func(int a)
{
return a*a;
}
#!/usr/bin/env python #test_so.pyfrom ctypes import cdll import os p = os.getcwd() + '/libfunc.so' f = cdll.LoadLibrary(p) print f.func(99)
$ gcc -fPIC -shared func.c -o libfunc.so $ ./test_so.py 9801
/* test.c */
#include <stdio.h>
int func(int a)
{
return a*a;
}
int main(int argc, char **argv)
{
int x;
sscanf(argv[1], "%d", &x);
printf("%d\n", func(x));
return 0;
}
#!/usr/bin/env python # test.py import sys x = int(sys.argv[1]) print x*x
/* test.c */
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *f;
char s[1024];
int ret;
f = popen("./test.py 99", "r");
while((ret=fread(s,1,1024,f))>0) {
fwrite(s,1,ret,stdout);
}
fclose(f);
return 0;
}
$ gcc test.c $ ./a.out 9801
def func(*a): res=1 for i in range(len(a)): res *= sum(a[i]) return res
#!/usr/bin/env python # test.py import colin def func(*a): res=1 for i in range(len(a)): res *= sum(a[i]) return res a = [1,2,3] b = [4,5,6] c = [7,8] d = [9] e = [10,11,12,13,14] f = colin.func2(99) g = colin.func3(a,b,c,d,e) h = func3(a,b,c,d,e) print "f = ",f print "g = ",g print "h = ",h
/* colin.h */
#ifndef Colin_h
#define Colin_h
typedef struct {
int *a;
int len;
} x_t;
typedef struct {
x_t *ax;
int len;
} y_t;
int func2(int a);
int func3(y_t *p);
void free_y_t(y_t *p);
#endif
/* colin.c */
#include "colin.h"
#include <stdlib.h>
int func2(int a)
{
return a*a;
}
int func3(y_t *p)
{
int result;
int sum;
int i, j;
result = 1;
for(i=0;i<p->len;i++) {
sum = 0;
for(j=0;j<p->ax[i].len;j++)
sum += p->ax[i].a[j];
result *= sum;
}
return result;
}
void free_y_t(y_t *p)
{
int i;
for(i=0;i<p->len;i++) {
free(p->ax[i].a);
}
free(p->ax);
}
/* wrap.c */
#include <Python.h>
#include <stdlib.h>
#include "colin.h"
PyObject* wrap_func2(PyObject* self, PyObject* args)
{
int n, result;
/* 从参数列表中导出一个整形,用"i" */
if (!PyArg_ParseTuple(args, "i", &n))
return NULL;
/* 用C语言的库实现来计算 */
result = func2(n);
/* 计算结果必须要导成python识别的类型 */
return Py_BuildValue("i", result);
}
PyObject* wrap_func3(PyObject* self, PyObject* args)
{
int n, result;
int i, j;
int size, size2;
PyObject *p,*q;
y_t *y;
y = malloc(sizeof(y_t));
/* 先数数有多少个参数,也就是列表的个数 */
size = PyTuple_Size(args);
/* 把数组的个数先分配了 */
y->len = size;
y->ax = malloc(sizeof(x_t)*size);
/* 遍历python里各个列表(参数) */
for(i=0;i<size;i++) {
/* 先获得第i个参数,是一个列表 */
p = PyTuple_GetItem(args, i);
/* 获得列表的长度 */
size2 = PyList_Size(p);
/* 为数组分配好空间 */
y->ax[i].len = size2;
y->ax[i].a = malloc(sizeof(int)*size2);
/* 遍历列表,依次把列表里的数转到数组里 */
for(j=0;j<size2;j++) {
q = PyList_GetItem(p, j);
PyArg_Parse(q,"i",&y->ax[i].a[j]);
}
}
/* 用C语言的库实现来计算 */
result = func3(y);
free_y_t(y);
free(y);
/* 结果转成python识别格式 */
return Py_BuildValue("i", result);
}
/* 这是接口列表,加载时是只加载此列表的地址,所以这个数据结构不能放栈(局部变量)内,会被清掉 */
static PyMethodDef colinMethods[] =
{
{"func2", wrap_func2, METH_VARARGS, "Just a test"},
{"func3", wrap_func3, METH_VARARGS, "Just a test"},
{NULL, NULL, METH_NOARGS, NULL}
};
/* python加载的时候的接口 */
/* 注意,既然库名叫colin,此函数必须交initcolin */
void initcolin()
{
PyObject *m;
m = Py_InitModule("colin", colinMethods);
}
$ gcc -I /usr/include/python2.7/ -fPIC -shared colin.c wrap.c -o colin.so $ ./test.py f = 9801 g = 729000 h = 729000
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有