
正文
python函数c python函数参数定义规则
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
python调用c函数
Python是解释性语言, 底层就是用c实现的, 所以用python调用C是很容易的, 下面就总结一下各种调用的方法, 给出例子, 所有例子都在ubuntu9.10, python2.6下试过
1. Python 调用 C (base)
想在python中调用c函数, 如这儿的fact
#include Python.h
int fact(int n)
{
if (n = 1)
return 1;
else
return n * fact(n - 1);
}
PyObject* wrap_fact(PyObject* self, PyObject* args)
{
int n, result;
if (! PyArg_ParseTuple(args, "i:fact", n))
return NULL;
result = fact(n);
return Py_BuildValue("i", result);
}
static PyMethodDef exampleMethods[] =
{
{"fact", wrap_fact, METH_VARARGS, "Caculate N!"},
{NULL, NULL}
};
void initexample()
{
PyObject* m;
m = Py_InitModule("example", exampleMethods);
}
把这段代码存为wrapper.c, 编成so库,
gcc -fPIC wrapper.c -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config
然后在有此so库的目录, 进入python, 可以如下使用
import example
example.fact(4)
2. Python 调用 C++ (base)
在python中调用C++类成员函数, 如下调用TestFact类中的fact函数,
#include Python.h
class TestFact{
public:
TestFact(){};
~TestFact(){};
int fact(int n);
};
int TestFact::fact(int n)
{
if (n = 1)
return 1;
else
return n * (n - 1);
}
int fact(int n)
{
TestFact t;
return t.fact(n);
}
PyObject* wrap_fact(PyObject* self, PyObject* args)
{
int n, result;
if (! PyArg_ParseTuple(args, "i:fact", n))
return NULL;
result = fact(n);
return Py_BuildValue("i", result);
}
static PyMethodDef exampleMethods[] =
{
{"fact", wrap_fact, METH_VARARGS, "Caculate N!"},
{NULL, NULL}
};
extern "C" //不加会导致找不到initexample
void initexample()
{
PyObject* m;
m = Py_InitModule("example", exampleMethods);
}
把这段代码存为wrapper.cpp, 编成so库,
g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config
然后在有此so库的目录, 进入python, 可以如下使用
import example
example.fact(4)
3. Python 调用 C++ (Boost.Python)
Boost库是非常强大的库, 其中的python库可以用来封装c++被python调用, 功能比较强大, 不但可以封装函数还能封装类, 类成员.
首先在ubuntu下安装boost.python, apt-get install libboost-python-dev
#include boost/python.hpp
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello)
{
using namespace boost::python;
def("greet", greet);
}
把代码存为hello.cpp, 编译成so库
g++ hello.cpp -o hello.so -shared -I/usr/include/python2.5 -I/usr/lib/python2.5/config -lboost_python-gcc42-mt-1_34_1
此处python路径设为你的python路径, 并且必须加-lboost_python-gcc42-mt-1_34_1, 这个库名不一定是这个, 去/user/lib查
然后在有此so库的目录, 进入python, 可以如下使用
import hello
hello.greet()
'hello, world'
4. python 调用 c++ (ctypes)
ctypes is an advanced ffi (Foreign Function Interface) package for Python 2.3 and higher. In Python 2.5 it is already included.
ctypes allows to call functions in dlls/shared libraries and has extensive facilities to create, access and manipulate simple and complicated C data types in Python - in other words: wrap libraries in pure Python. It is even possible to implement C callback functions in pure Python.
#include Python.h
class TestFact{
public:
TestFact(){};
~TestFact(){};
int fact(int n);
};
int TestFact::fact(int n)
{
if (n = 1)
return 1;
else
return n * (n - 1);
}
extern "C"
int fact(int n)
{
TestFact t;
return t.fact(n);
}
将代码存为wrapper.cpp不用写python接口封装, 直接编译成so库,
g++ -fPIC wrapper.cpp -o example.so -shared -I/usr/include/python2.6 -I/usr/lib/python2.6/config
进入python, 可以如下使用
import ctypes
pdll = ctypes.CDLL('/home/ubuntu/tmp/example.so')
pdll.fact(4)
12
相关问答
Q1: Python 如何给 c 函数传递结构体参数
//test1.c# include stdio.h# include stdlib.hstruct Student
{ char name[30]; float fScore[3];
};void Display(struct Student su){ printf("-----Information------\n"); printf("Name:%s",su.name); printf("Chinese:%.2f\n",su.fScore[0]); printf("Math:%.2f\n",su.fScore[1]); printf("English:%.2f",su.fScore[2]); printf("平均分数为:%.2f\n",(su.fScore[0]+su.fScore[1],su.fScore[2])/3);
}
Q2: python与c区别有哪些
第一点:语言类型不同
python是一种动态类型语言,又是强类型语言。它们确定一个变量python函数c的类型是在您第一次给它赋值python函数c的时候。C是静态类型语言,一种在编译期间就确定数据类型的语言。大多数静态类型语言是通过要求在使用任一变量之前声明其数据类型来保证这一点的。
第二点:对象机制不同
python中所有的数据,都是由对象或者对象之间的关系表示的,函数是对象,字符串是对象,每个东西都是对象的概念。每一个对象都有三种属性:实体、类型和值。C中没有对象这个概念,只有数据的表示。
第三点:变量类型不同
python六个标准的数据类型:Number数字、string字符串、List列表、Tuple元组、Sets集合、Dictionary字典,数字类型有四种类型:整数、布尔型、浮点数和复数。C语言也可分四类:基本类型、枚举类型、void的类型、派生类型,基本类型:整数类型、浮点类型。
第四点:函数库的使用方法不同
在C语言中使用那个库函数,需要引入头文件用include引入,而在python中需要引入别的模块或者函数时需要用import引入。
两者的不同机制是C语言中include是告诉预处理器,这个include指定的文件的内容,要当作本地源文件出现过,而python中的import可以通过简单的import引入,或者是import
numpy as np。
第五点:全局变量不同
在C语言中,声明全局变量,如果值是恒定的,那么可以直接用#define声明,如果只是声明全局,并且变量的值是可变的,那么直接类似int
a就可以python函数c了。
在python中,声明全局变量时,需要加上global,类似global a,在函数里面使用的时候需要先声明global
a,否则直接用a那么python会重新创建一个新的本地对象并将新的值赋值给python函数c他,原来的全局变量的值并不变化。
Q3: python使用ctypes调用C编译dll函数方法
在函数声明加入前缀,如
__declspec(dllexport) int Fun(int a, int b)
否则在加载该dll时会提示找不到该符号
在windows下可以通过vs自带的dumpbin工具查看可被调用符号
dumpbin /exports test.dll
C函数在调用过程中关于参数传递和压栈由多种规定,作为dll提供给其他程序调用时,必须明确并统一为同一种调用规定,否则会导致栈破坏,编译器负责具体实现调用规定,主要有以下几种调用规定
python下调用C库有多种方式,ctypes是其中一种比较方便的,调用时首先需要加载dll文件,根据C dll的调用规定不同需要使用不同接口,使用ctypes需要 import ctypes 库
对于简单的C函数,例如 int add(int a, int b) , 此时就可以直接调用了,如
对于较复杂的C函数的参数情况,ctypes调用时对入参和出餐做一定处理,这里分情况讨论
以上包含了几种主要的参数传递情况,ctypes也提供了一个较为完整的python类型和C类型的对照,如下:
Q4: 如何用python获得C函数中的变量的值?
网页链接
python ctypes 第一句话:
ctypes is a foreign function library for Python. It provides C compatible data types, and allows calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python.
没看到有说可以直接访问data。
你可以这样加这样的函数:
int get_a()
{
return a;
}
再在python里面调用。
python函数c的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于python函数参数定义规则、python函数c的信息别忘了在本站进行查找喔。







