将Python安装目录下的include和libs文件夹引入到项目中,将libs目录下的python37.lib复制一份为python37_d.lib
1 2 3 4 5 |
def Hello(): print("Hello")
def Add(a,b): return a+b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#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中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#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(); } |
python调用c++一种是基于extern 的方式,另一种是swig
初级版:
首先先看一下Python调用c
C代码:
1 2 3 4 5 6 7 |
#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代码:
1 2 3 4 |
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++代码:
1 2 3 4 5 6 7 8 9 10 11 |
#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代码:
1 2 3 4 |
import ctypes lib = ctypes.CDLL("./libpycall.so") lib.foo_(1, 3) print '***finish***' |
编译:
g++ -g -o libpycall.so -shared -fPIC pycall.cpp
升级版:
c++定义一个类,通过python调用c++类的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#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 代码:
1 2 3 4 5 6 |
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是一种软件开发工具,能让一些脚本语言调用C/C++语言的接口。它实现的方法是,通过编译程序将C/C++的声明文件(.i文件)编译成C/C++的包装器源代码(.c或.cxx)。通过直接调用这样的包装器接口,脚本语言可以间接调用C/C++语言的程序接口。
参考地址:https://github.com/swig/swig
首先安装,源码或者pip
案例:
有这样一段C的代码,文件名为example.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/* 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:(这里的格式非常重要,即使第一行的注释也不能省略)
1 2 3 4 5 6 7 8 9 10 11 12 |
/* 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个部分:
接下来以linux操作系统下,为python语言生成接口为例:
1 |
swig -python example.i |
执行上述语句会生成两个文件example.py和example_wrap.c。example.py就是python语言可以调用的example模块,而example_wrap.c则封装了example.c的封装器。
然后执行第二步:
1 |
gcc -c -fPIC example.c example_wrap.c -I/usr/include/python2.7 |
执行该步会生成两个o文件,example.o和example_wrap.o。
最后执行:
1 |
g++ -shared example.o example_wrap.o -o _example.so |
这一步会将上面两个o文件封装成一个新的动态库,_example.so。在这之后就可以在python内直接调用example.c提供的接口了。
1 2 3 |
import example print example.fact(3) print example.cvar.My_variable #注意这里的参数不能直接用,得用cvar |