python 和c++相互调用
2022/1/27 17:06:25
本文主要是介绍python 和c++相互调用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
c++调用Python
将Python安装目录下的include和libs文件夹引入到项目中
将libs目录下的python37.lib复制一份为python37_d.lib
- Python脚本
def Hello(): print("Hello") def Add(a,b): return a+b
- C++调用python脚本
#include <Python.h> using namespace std; int main() { Py_Initialize(); //初始化,创建一个Python虚拟环境 if (Py_IsInitialized()) { PyObject* pModule = NULL; PyObject* pFunc = NULL; pModule = PyImport_ImportModule("test_python"); //参数为Python脚本的文件名 if (pModule) { pFunc = PyObject_GetAttrString(pModule, "Hello"); //获取函数 PyEval_CallObject(pFunc, NULL); //执行函数 } else { printf("导入Python模块失败...\n"); } } else { printf("Python环境初始化失败...\n"); } Py_Finalize(); }
接口方法
Python3.6提供给C/C++接口函数,基本都是定义pylifecycle.h,pythonrun.h,ceval.h中。
- Py_Initialize() 和 Py_Finalize()
必须先调用Py_Initialize()进行初始化,这个API用来分配Python解释器使用的全局资源,应用程序结束时需要调用Py_Finalize()来关闭Python的使用环境。 - Py_IsInitialized()
用来判断Python解释器是否初始化成功,true为成功,false为失败。 - PyErr_Print() & PyErr_Clear()
执行Python出错时,PyErr_Print()可将错误信息显示出来,PyErr_Clear()将错误信息在Python解释器的缓存清除。 - PyRun_SimpleString()
这个函数能够用来执行简单的Python语句。 - PyEval_InitThreads()
如果使用多线程调用Python脚本,就需要在初始化Python解释器时调用PyEval_InitThreads()来启用线程支持(导致Python内部启用线程锁),最好在主线程启动时就调用。该API同时也锁定全局解释锁,所以,还需要在初始化完成后需要自行释放锁。
如果不需要使用多线程,不建议启用该选项,互斥锁也会不可避免的增加系统开销。
3.规范化语法
#include<Python.h> //添加python的声明 using namespace std; int main() { Py_Initialize(); //1、初始化python接口 //初始化使用的变量 PyObject* pModule = NULL; PyObject* pFunc = NULL; PyObject* pName = NULL; //2、初始化python系统文件路径,保证可以访问到 .py文件 PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append('./')"); //3、调用python文件名。当前的测试python文件名是test.py。在使用这个函数的时候,只需要写文件的名称就可以了。不用写后缀。 pModule = PyImport_ImportModule("test"); //4、调用函数 pFunc = PyObject_GetAttrString(pModule, "AdditionFc"); //5、给python传参数 PyObject* pArgs = PyTuple_New(2);//函数调用的参数传递均是以元组的形式打包的,2表示参数个数。如果AdditionFc中只有一个参数时,写1就可以了。这里只先介绍函数必须有参数存在的情况。 PyTuple_SetItem(pArgs, 0, Py_BuildValue("i", 2)); //0:表示序号。第一个参数。 PyTuple_SetItem(pArgs, 1, Py_BuildValue("i", 4)); //1:也表示序号。第二个参数。i:表示传入的参数类型是int类型。 //6、使用C++的python接口调用该函数 PyObject* pReturn = PyEval_CallObject(pFunc, pArgs); //7、接收python计算好的返回值 int nResult; PyArg_Parse(pReturn, "i", &nResult);//i表示转换成int型变量。在这里,最需要注意的是:PyArg_Parse的最后一个参数,必须加上“&”符号。 //8、结束python接口初始化 Py_Finalize(); }
Pthon调用c++
python调用c++一种是基于extern 的方式,另一种是swig
基于extern
初级版
首先先看一下Python调用c
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; }
Python代码
import ctypes lib = ctypes.CDLL("./libpycall_c.so") lib.foo(1, 3) print '***finish***'
编译:
gcc -g -o libpycall_c.so -shared -fPIC pycall_c.c
然后基于c++改造上述代码(使用g++编译生成C动态库的代码中的函数或者方法,需要使用extern “C”来进行编译)
c++代码:
#include <iostream> using namespace std; int foo(int a, int b){ cout << "the number you input:" << a << "\t" << b << endl; return a + b; } extern "C" { int foo_(int a, int b){ foo(a, b); } }
python代码:
import ctypes lib = ctypes.CDLL("./libpycall.so") lib.foo_(1, 3) print '***finish***'
编译:
g++ -g -o libpycall.so -shared -fPIC pycall.c
升级版
c++定义一个类,通过python调用c++类的方法
#include <iostream> using namespace std; class TestLib{ private: int number = 0; public: void set_number(int num){ number = num; } int get_number(){ return number; } }; extern "C" { TestLib obj; int get_number(){ return obj.get_number(); } void set_number(int num){ obj.set_number(num); } }
python 代码:
import ctypes lib = ctypes.CDLL("./libpycall.so") print lib.get_number() #0 lib.set_number(10) print lib.get_number() #10
编译
g++ -g -o libpycall.so -shared -fPIC -std=c++11 pycall.cpp
基于swig
Swig是一种软件开发工具,能让一些脚本语言调用C/C++语言的接口。它实现的方法是,通过编译程序将C/C++的声明文件(.i文件)编译成C/C++的包装器源代码(.c或.cxx)。通过直接调用这样的包装器接口,脚本语言可以间接调用C/C++语言的程序接口。
参考地址:https://github.com/swig/swig
首先安装,源码或者pip
案例:
假如你有这样一段C的代码,文件名为example.c
/* File : example.c */ double My_variable = 3.0; /* Compute factorial of n */ int fact(int n) { if (n <= 1) return 1; else return n*fact(n-1); } /* Compute n mod m */ int my_mod(int n, int m) { return(n % m); }
你想在你的脚本语言的代码里面调用fact函数。你可以通过一段非常简单的SWIG脚本,文件名为example.i:(这里的格式非常重要,即使第一行的注释也不能省略)
/* File : example.i */ %module example %{ /* Put headers and other declarations here */ extern double My_variable; extern int fact(int); extern int my_mod(int n, int m); %} extern double My_variable; extern int fact(int); extern int my_mod(int n, int m);
这段.i文件分成3个部分:
第一部分是 %module example, %module是SWIG脚本的一个命令,它表示生成的包装器将在一个模块内的名称。
第二部分是%{… %},这一部分的内容会原封不动的插入到xxxx_wrap.c或xxxx_wrap.cxx文件中。
第三部分就是剩下的部分了。这部分就是C语言或者C++语言的接口声明了。和C/C++的语法是一样的。
接下来以linux操作系统下,为python语言生成接口为例:
swig -python example.i
执行上述语句会生成两个文件example.py和example_wrap.c。 example.py就是python语言可以调用的example模块,而example_wrap.c则封装了example.c的封装器。
然后执行第二步:
gcc -c -fPIC example.c example_wrap.c -I/usr/include/python2.7
执行该步会生成两个o文件,example.o和example_wrap.o。
最后执行:
g++ -shared example.o example_wrap.o -o _example.so
这一步会将上面两个o文件封装成一个新的动态库,_example.so。在这之后就可以在python内直接调用example.c提供的接口了。
import example print example.fact(3) print example.cvar.My_variable #注意这里的参数不能直接用,得用cvar
参考:https://blog.csdn.net/LordofRobots/article/details/77870862
这篇关于python 和c++相互调用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python