网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

python3调用c函数 c++调用python3

python 怎么调用c语言接口

ctypes:  可直接调用c语言动态链接库。

创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计制作、做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的镇康网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

使用步骤:

1 编译好自己的动态连接库

2 利用ctypes载入动态连接库

3 用ctype调用C函数接口时,需要将python变量类型做转换后才能作为函数参数,转换原则见下图:

4 Python若想获取ctypes调用的C函数返回值,需要先指定返回值类型。我们将在接下来的完整Sample中看到如何使用。

#Step 1:  test.c#include stdio.h

int add(int a, int b)

{

return a + b;

}#Step 2: 编译动态链接库 ( 如何编译动态链接库在本文不详解,网上资料一大堆。)gcc -fPIC -shared test.c -o libtest.so  

#Step 3:  test.py

from ctypes import *mylib = CDLL("libtest.so")   或者   cdll.LoadLibrary("libtest.so")   add = mylib.add

add.argtypes = [c_int, c_int] # 参数类型,两个int(c_int是ctypes类型,见上表)

add.restype = c_int # 返回值类型,int (c_int 是ctypes类型,见上表)

sum = add(3, 6)

怎样让Python脚本与C++程序互相调用

二、Python调用C/C++

1、Python调用C动态链接库

Python调用C库比较简单,不经过任何封装打包成so,再使用python的ctypes调用即可。

(1)C语言文件:pycall.c

[html] view plain copy

/***gcc -o libpycall.so -shared -fPIC pycall.c*/

#include stdio.h

#include stdlib.h

int foo(int a, int b)

{

printf("you input %d and %d\n", a, b);

return a+b;

}

(2)gcc编译生成动态库libpycall.so:gcc -o libpycall.so -shared -fPIC pycall.c。使用g++编译生成C动态库的代码中的函数或者方法时,需要使用extern "C"来进行编译。

(3)Python调用动态库的文件:pycall.py

[html] view plain copy

import ctypes

ll = ctypes.cdll.LoadLibrary

lib = ll("./libpycall.so")

lib.foo(1, 3)

print '***finish***'

(4)运行结果:

2、Python调用C++(类)动态链接库

需要extern "C"来辅助,也就是说还是只能调用C函数,不能直接调用方法,但是能解析C++方法。不是用extern "C",构建后的动态链接库没有这些函数的符号表。

(1)C++类文件:pycallclass.cpp

[html] view plain copy

#include iostream

using namespace std;

class TestLib

{

public:

void display();

void display(int a);

};

void TestLib::display() {

cout"First display"endl;

}

void TestLib::display(int a) {

cout"Second display:"aendl;

}

extern "C" {

TestLib obj;

void display() {

obj.display();

}

void display_int() {

obj.display(2);

}

}

(2)g++编译生成动态库libpycall.so:g++ -o libpycallclass.so -shared -fPIC pycallclass.cpp。

(3)Python调用动态库的文件:pycallclass.py

[html] view plain copy

import ctypes

so = ctypes.cdll.LoadLibrary

lib = so("./libpycallclass.so")

print 'display()'

lib.display()

print 'display(100)'

lib.display_int(100)

(4)运行结果:

3、Python调用C/C++可执行程序

(1)C/C++程序:main.cpp

[html] view plain copy

#include iostream

using namespace std;

int test()

{

int a = 10, b = 5;

return a+b;

}

int main()

{

cout"---begin---"endl;

int num = test();

cout"num="numendl;

cout"---end---"endl;

}

(2)编译成二进制可执行文件:g++ -o testmain main.cpp。

(3)Python调用程序:main.py

[html] view plain copy

import commands

import os

main = "./testmain"

if os.path.exists(main):

rc, out = commands.getstatusoutput(main)

print 'rc = %d, \nout = %s' % (rc, out)

print '*'*10

f = os.popen(main)

data = f.readlines()

f.close()

print data

print '*'*10

os.system(main)

(4)运行结果:

4、扩展Python(C++为Python编写扩展模块)

所有能被整合或导入到其它python脚本的代码,都可以被称为扩展。可以用Python来写扩展,也可以用C和C++之类的编译型的语言来写扩展。Python在设计之初就考虑到要让模块的导入机制足够抽象。抽象到让使用模块的代码无法了解到模块的具体实现细节。Python的可扩展性具有的优点:方便为语言增加新功能、具有可定制性、代码可以实现复用等。

为 Python 创建扩展需要三个主要的步骤:创建应用程序代码、利用样板来包装代码和编译与测试。

(1)创建应用程序代码

[html] view plain copy

#include stdio.h

#include stdlib.h

#include string.h

int fac(int n)

{

if (n 2) return(1); /* 0! == 1! == 1 */

return (n)*fac(n-1); /* n! == n*(n-1)! */

}

char *reverse(char *s)

{

register char t, /* tmp */

*p = s, /* fwd */

*q = (s + (strlen(s) - 1)); /* bwd */

while (p q) /* if p q */

{

t = *p; /* swap move ptrs */

*p++ = *q;

*q-- = t;

}

return(s);

}

int main()

{

char s[BUFSIZ];

printf("4! == %d\n", fac(4));

printf("8! == %d\n", fac(8));

printf("12! == %d\n", fac(12));

strcpy(s, "abcdef");

printf("reversing 'abcdef', we get '%s'\n", \

reverse(s));

strcpy(s, "madam");

printf("reversing 'madam', we get '%s'\n", \

reverse(s));

return 0;

}

上述代码中有两个函数,一个是递归求阶乘的函数fac();另一个reverse()函数实现了一个简单的字符串反转算法,其主要目的是修改传入的字符串,使其内容完全反转,但不需要申请内存后反着复制的方法。

(2)用样板来包装代码

接口的代码被称为“样板”代码,它是应用程序代码与Python解释器之间进行交互所必不可少的一部分。样板主要分为4步:a、包含Python的头文件;b、为每个模块的每一个函数增加一个型如PyObject* Module_func()的包装函数;c、为每个模块增加一个型如PyMethodDef ModuleMethods[]的数组;d、增加模块初始化函数void initModule()。

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相互调用】python里的dict如何作为参数传入.so中的c语言函数

#include stdio.h

#include stdlib.h

#include Python.h

static PyObject *

wmf_reverse(PyObject *self, PyObject *args, PyObject *kwargs) { 

static char* kwlist[] = {"name", NULL};

char *name = NULL;

PyObject *retval = NULL; 

// 问题1: 只取一个字符串,format应该是"s"

//  if(PyArg_ParseTupleAndKeywords(args,keyds,"isi",kwlist,name))

if (PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, name)) {

retval = (PyObject *)Py_BuildValue("i",1);

printf("%s\n", name);

// 问题2:不要释放

//  free(name); 

} else {

retval = (PyObject *)Py_BuildValue("i",0);

}

return retval;

static PyMethodDef

wmf_methods[] = {

{"reverse",(PyCFunction)wmf_reverse, METH_VARARGS | METH_KEYWORDS, "reverse"},

// 问题3:方法定义表,应该用一条空记录来表示结束。

{NULL, NULL, 0, NULL},

};

// 问题4:没有定义module

static struct PyModuleDef

wmf_module = {

PyModuleDef_HEAD_INIT,

"wmf",      /* name of module */

NULL,       /* module documentation, may be NULL */

-1,         /* size of per-interpreter state of the module,

or -1 if the module keeps state in global variables. */

wmf_methods,

};

// 问题5:入口函数要声明为:PyMODINIT_FUNC

PyMODINIT_FUNC

PyInit_wmf(void) {

// 问题6:Py_InitModule要初始化的是模块,不是方法。所以传方法定义是错误的。

// 另外,python2.x是用Py_Init_module,python3.x改用PyModule_Create了。

// 两者略有差别,自己注意一下吧。这里我用的是python3.x。

//Py_InitModule("wmf",ExtestMethods);

PyObject *m;

m = PyModule_Create(wmf_module);

if (m == NULL) {

return NULL;

}

return m;

}

python怎么调用c的main函数

if

__name__=="__main__":

print

'main'

当脚本作为执行脚本时__name__的值为__main__当脚本作为模块时__name__为模块文件名。举个例子,a.py作为执行脚本时__name__的值是__main__。有2个脚本,a.py和b.py,a中引入b,执行a.py时,在b中模块的__name__就是b.py


本文标题:python3调用c函数 c++调用python3
网站URL:http://bjjierui.cn/article/hpedsj.html

其他资讯