RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
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

如何让python调用C和C++代码

二、Python调用C/C++1、Python调用C动态链接库Python调用C库比较简单,不经过任何封装打包成so,再使用python的ctypes调用即可。(1)C语言文件:pycall.c[html]viewplaincopy/***gcc-olibpycall.so-shared-fPICpycall.c*/#include#includeintfoo(inta,intb){printf("youinput%dand%d\n",a,b);returna+b;}(2)gcc编译生成动态库libpycall.so:gcc-olibpycall.so-shared-fPICpycall.c。使用g++编译生成C动态库的代码中的函数或者方法时,需要使用extern"C"来进行编译。(3)Python调用动态库的文件:pycall.py[html]viewplaincopyimportctypesll=ctypes.cdll.LoadLibrarylib=ll("./libpycall.so")lib.foo(1,3)print'***finish***'(4)运行结果:2、Python调用C++(类)动态链接库需要extern"C"来辅助,也就是说还是只能调用C函数,不能直接调用方法,但是能解析C++方法。不是用extern"C",构建后的动态链接库没有这些函数的符号表。(1)C++类文件:pycallclass.cpp[html]viewplaincopy#includeusingnamespacestd;classTestLib{public:voiddisplay();voiddisplay(inta);};voidTestLib::display(){cout#include#includeintfac(intn){if(n2)return(1);/*0!==1!==1*/return(n)*fac(n-1);/*n!==n*(n-1)!*/}char*reverse(char*s){registerchart,/*tmp*/*p=s,/*fwd*/*q=(s+(strlen(s)-1));/*bwd*/while(p

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类型的对照,如下:

python怎样嵌入c

用c语言编写一个动态库,提供两个函数,两个数的整形求和,两个浮点数的求和。取名为mylib.c。

将c函数文件编译成so动态库。运行gcc mylib.c -fPIC -shared -o libtest.so命令,在目录下可以看到生成的库文件libtest.so。

Python调用so库文件。首先导入ctypes,其次用CDLL加载so文件,最后调用对应的函数。将python代码保存到pydemo.py中。

执行python pydemo.py查看运行结果。

众多python培训视频,尽在python学习网,欢迎在线学习!

python怎么调用c的动态链接库

Python调用C/C++动态链接库的需求

在自动化测试过程中,难免会遇到语言混合使用的情况,这不,我们也遇到了。初步决定采用Robot Framework作为自动化测试框架后,其支持Java和Python,而Python作为主流的语言,怎么能放弃使用它的机会^_^。 然而产品采用是古老90年代开发的C/S结构,因为古老,当时也没有考虑到对产品的测试进行自动化,Client端并没有预留CLI(Command Line interface)形式的接口,真是雪上加霜啊。

那怎么自动化?采用AutoIT来对客户端界面进行自动化测试?可惜AutoIT对当初开发采用的控件识别不是很好,如果采用控件所在位置来进行控制的方式,又会导致自动化测试并不是很稳定。那么!!!只有自己开发接口了,目前在Client端开发出CLI形式的接口,将其封装为DLL,然后在Robot FrameWork框架中采用Python对DLL进行调用。任务艰巨哪!

Python调用DLL例子

示例一

首先,在创建一个DLL工程(本人是在VS 2005中创建),头文件:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片//hello.h

#ifdef EXPORT_HELLO_DLL

#define HELLO_API __declspec(dllexport)

#else

#define HELLO_API __declspec(dllimport)

#endif

extern "C"

{

HELLO_API int IntAdd(int , int);

}

CPP文件:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片//hello.cpp

#define EXPORT_HELLO_DLL

#include "hello.h"

HELLO_API int IntAdd(int a, int b)

{

return a + b;

}

这里有两个注意点:

(1)弄清楚编译的时候函数的调用约定采用的__cdecl还是__stdcall,因为根据DLL中函数调用约定方式,Python将使用相应的函数加载DLL。

(2)如果采用C++的工程,那么导出的接口需要extern "C",这样python中才能识别导出的函数。

我的工程中采用__cdecl函数调用约定方式进行编译链接产生hello.dll,然后Python中采用ctypes库对hello.dll进行加载和函数调用:

[python] view plain copy 在CODE上查看代码片派生到我的代码片from ctypes import *

dll = cdll.LoadLibrary('hello.dll');

ret = dll.IntAdd(2, 4);

print ret;

OK,一个小例子已经完成了,如果你感兴趣,但还没试过,那就尝试一下吧。

示例二

示例一只是一个"hello world"级别的程序,实际运用中更多的需要传递数据结构、字符串等,才能满足我们的需求。那么这个示例将展示,如何传递数据结构参数,以及如何通过数据结构获取返回值。

首先编写DLL工程中的头文件:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片//hello.h

#ifdef EXPORT_HELLO_DLL

#define HELLO_API __declspec(dllexport)

#else

#define HELLO_API __declspec(dllimport)

#endif

#define ARRAY_NUMBER 20

#define STR_LEN 20

struct StructTest

{

int number;

char* pChar;

char str[STR_LEN];

int iArray[ARRAY_NUMBER];

};

extern "C"

{

//HELLO_API int IntAdd(int , int);

HELLO_API char* GetStructInfo(struct StructTest* pStruct);}

CPP文件如下:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片//hello.cpp

#include string.h

#define EXPORT_HELLO_DLL

#include "hello.h"

HELLO_API char* GetStructInfo(struct StructTest* pStruct){

for (int i = 0; i ARRAY_NUMBER; i++)

pStruct-iArray[i] = i;

pStruct-pChar = "hello python!";

strcpy (pStruct-str, "hello world!");

pStruct-number = 100;

return "just OK";

}

GetStructInfo这个函数通过传递一个StructTest类型的指针,然后对对象中的属性进行赋值,最后返回"just OK".

编写Python调用代码如下,首先在Python中继承Structure构造一个和C DLL中一致的数据结构StructTest,然后设置函数GetStructInfo的参数类型和返回值类型,最后创建一个StructTest对象,并将其转化为指针作为参数,调用函数GetStrcutInfo,最后通过输出数据结构的值来检查是否调用成功:

[python] view plain copy 在CODE上查看代码片派生到我的代码片from ctypes import *

ARRAY_NUMBER = 20;

STR_LEN = 20;

#define type

INTARRAY20 = c_int * ARRAY_NUMBER;

CHARARRAY20 = c_char * STR_LEN;

#define struct

class StructTest(Structure):

_fields_ = [

("number", c_int),

("pChar", c_char_p),

("str", CHARARRAY20),

("iArray", INTARRAY20)

]

#load dll and get the function object

dll = cdll.LoadLibrary('hello.dll');

GetStructInfo = dll.GetStructInfo;

#set the return type

GetStructInfo.restype = c_char_p;

#set the argtypes

GetStructInfo.argtypes = [POINTER(StructTest)];objectStruct = StructTest();

#invoke api GetStructInfo

retStr = GetStructInfo(byref(objectStruct));#check result

print "number: ", objectStruct.number;

print "pChar: ", objectStruct.pChar;

print "str: ", objectStruct.str;

for i,val in enumerate(objectStruct.iArray):

print 'Array[i]: ', val;

print retStr;

总结

1. 用64位的Python去加载32位的DLL会出错

2. 以上只是些测试程序,在编写Python过程中尽可能的使用"try Except"来处理异常3. 注意在Python与C DLL交互的时候字节对齐问题4. ctypes库的功能还有待继续探索


网站标题:Python加载c库函数 python引入函数库
网站地址:http://scyingshan.cn/article/doopjdj.html