资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c调用python找函数,Python调用c函数

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

求助 关于c程序中嵌入Python的问题

嵌入

与python的扩展相对,嵌入是把Python解释器包装到C的程序中。这样做可以给大型的,单一的,要求严格的,私有的并且(或者)极其重要的应用程序内嵌Python解释器的能力。一旦内嵌了Python,世界完全不一样了。

C调用python中的函数:

hw.py:

#coding=utf8

def hw_hs(canshu):

return canshu

if __name__ == "__main__":

ccss = "I am hw"

print hw_hs(ccss)

helloWorld.py:

#coding=utf8

import hw

def hello():

ccss = "I am helloWorld"

return hw.hw_hs(ccss)

if __name__ == "__main__":

print hello()

testcpypy.c:

//#include "testcpypy.h"

#include Python.h

#include stdio.h

#include stdlib.h

int main()

{

//初始化Python

Py_Initialize();

if (!Py_IsInitialized()) {

printf("Py_Initialize");

getchar();

return -1;

}

//执行python语句

PyRun_SimpleString("import sys");

PyRun_SimpleString("sys.path.append('./')");

PyObject *pModule = NULL;

PyObject *pFunc = NULL;

PyObject *reslt =NULL;

//载入python模块

if(!(pModule = PyImport_ImportModule("helloWorld"))) {

printf("PyImport_ImportModule");

getchar();

return -1;

}

//查找函数

pFunc = PyObject_GetAttrString(pModule, "hello");

if ( !pFunc || !PyCallable_Check(pFunc) )

{

printf("can't find function [hello]");

getchar();

return -1;

}

//调用python中的函数

reslt = (PyObject*)PyEval_CallObject(pFunc, NULL);

//printf("function return value : %d\r\n", PyInt_AsLong(reslt));

//将python返回的对象转换为C的字符串

char *resltc=NULL;

int res;

res = PyArg_Parse(reslt, "s", resltc);

if (!res) {

printf("PyArg_Parse");

getchar();

return -1;

}

printf("resltc is %s", resltc);

getchar();

//释放内存

Py_DECREF(reslt);

Py_DECREF(pFunc);

Py_DECREF(pModule);

//关闭python

Py_Finalize();

return 0;

}

编译:

gcc -o testcpypy testcpypy.c -IC:\Python27\include -LC:\Python27\libs -lpython27 ---C:\Python27为python安装目录

或:

gcc -c testcpypy.c -IC:\Python27\include

gcc -o testcpypy.exe testcpypy.o -LC:\Python27\libs -lpython27

执行结果:

带参数的情况:

#include "callpydll.h"

#include "Python.h"

#include stdio.h

#include stdlib.h

#include string.h

#include stdarg.h

int callhello(char *instr, char *outstr)

{

PyObject *pModule = NULL;

PyObject *pFunc = NULL;

PyObject *reslt = NULL;

PyObject *pParm = NULL;

char *resltc = NULL;

int resltn;

int res;

char *helloWorld = "TestIM_ProtocBuf";

char *im_account = "aaaa";

char *auth_code = "aaaa";

char *im_uid = "aaaa";

char *proxy_topic = "";

//初始化Python

Py_Initialize();

if (!Py_IsInitialized()) {

printf("Py_Initialize");

getchar();

return -1;

}

//执行python语句

PyRun_SimpleString("import sys");

PyRun_SimpleString("sys.path.append('./')");

//载入python模块

if(!(pModule = PyImport_ImportModule(helloWorld))) {

printf("PyImport_ImportModule");

getchar();

return -2;

}

//查找函数

pFunc = PyObject_GetAttrString(pModule, "login_proxy_body_serialize");

if ( !pFunc || !PyCallable_Check(pFunc) )

{

printf("can't find function [hello]");

getchar();

return -3;

}

//参数转换C -- python, 参数必须是元组(一个参数也是,否则会失败!!!坑啊)

pParm = Py_BuildValue("(ssss)", im_account, auth_code, im_uid, proxy_topic);

//调用python中的函数

reslt = (PyObject*)PyEval_CallObject(pFunc, pParm);

//将python返回的对象转换为C的字符串

res = PyArg_ParseTuple(reslt, "si", resltc, resltn);

if (!res) {

printf("PyArg_Parse");

getchar();

return -4;

}

printf("resltn is %d", resltn);

memcpy(outstr, resltc, strlen(resltc)+1);

//释放内存

Py_DECREF(reslt);

Py_DECREF(pFunc);

Py_DECREF(pModule);

Py_DECREF(pParm);

//关闭python

Py_Finalize();

return 0;

}

int main() {

int i;

char *dais = "iammain";

char res[10240];

memset(res,'\0',sizeof(res));

i = callhello(dais, res);

if(0 != i) {

printf("Notify:error");

getchar();

return -1;

}

printf("result is %s", res);

getchar();

return 0;

}

C中调用Python函数,找不到模块

是因为你的模块的路径不对,必须先指定路径 PyObject *sys = PyImport_ImportModule("sys"); PyObject *path = PyObject_GetAttrString(sys, "path"); PyList_Append(path, PyString_FromString(""));C中调用Python函数,找不到模块

C语言程序如何调用python程序

下面是一个例子:

首先是python的一个简单函数

class Hello:

def __init__(self, x):

self.a = x

def print(self, x=None):

print(x)

def xprint():

print("hello world")

if __name__ == "__main__":

xprint()

h = Hello(5)

h.print()1

下面是C语言

#include python3.4m/Python.h

#include stdio.h

#include stdlib.h

#include string.h

int main()

{

Py_Initialize();

// 将当前目录加入sys.path

PyRun_SimpleString("import sys");

PyRun_SimpleString("sys.path.append('./')");

// 导入hello.py模块

PyObject *pmodule = PyImport_ImportModule("hello");

// 获得函数xprint对象,并调用,输出“hello world\n”

PyObject *pfunc = PyObject_GetAttrString(pmodule, "xprint");

PyObject_CallFunction(pfunc, NULL);

// 获得类Hello并生成实例pinstance,并调用print成员函数,输出“5 6\n”

PyObject *pclass = PyObject_GetAttrString(pmodule, "Hello");

PyObject *arg = Py_BuildValue("(i)", 5);

PyObject *pinstance = PyObject_Call(pclass, arg, NULL);

PyObject_CallMethod(pinstance, "print", "i", 6);

Py_Finalize();

return 0;

}

编译命令如下:

gcc pyapi.c -lpython3.4m -o pyapi

段错误当试图从C调用Python函数问题,怎么解决

简单看了一下你的程序

首先,你没有给出完整的程序,这个让大伙看起来确实不方便,因为没有main函数,可能踩内存的地方并不是这个函数本身,是main函数

其次说你的这个函数,貌似确实有点问题,但是由于没有完整的程序,我只说出我的理解,如果有错,直接忽视就好

sprintf(flag_height,"%03d",i+1);

sprintf(flag_width,"%03d",j+1);

我觉得问题出在这里,你查下 sprintf 的用法,第一个参数应该是一个字符串的首地址,你写的确实没有错,参数给入的是一个地址flag_height,但是纵观你的程序,你并没有给这个指针赋值,程序运行到这里,估计在默认创建指针的时候应该赋值为NULL(貌似根据编译器不同而有不同),那个寻址后报出段错误,也是意料之中了

由于没有你完整的代码,不知道你要做什么,修改意见不好提

给出个人意见,仅供参考

如果要打印flag_height中的值,首先应该给flag_height指针分配一片内存,使用malloc函数,其次在向内从中写入值,然后调用sprintf,就应该没有问题了


本文题目:c调用python找函数,Python调用c函数
文章出自:http://cdkjz.cn/article/hdchgh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220