文章目录
- 前提
- ctypes
- pybind11
- 在python中调用C++
- 在C++中调用python
- Cython
- 加快python速度
- 在C中调用python代码
- 调用Python的原生C API
- 参考链接
前提
因项目需求,需要在C++中调用python,对这方面的一些工具做个简单的介绍。
ctypes
ctypes 是 Python 的外部函数库。它提供了与 C 兼容的数据类型,并允许调用 DLL 或共享库中的函数。可使用该模块以纯 Python 形式对这些库进行封装。
上面是ctypes官方文档给出的介绍,通俗理解来说:ctypes可以加载动态链接库,然后以此调用动态链接库中的函数。也就是说,如果我们有一个.c
文件,我们可以将它编译成库,然后在python代码里面使用ctypes加载调用它。
相关代码如下:
- 创建一个
main.c
文件,包括三个函数,等会我们要通过调用动态链接库的方式在python中调用这三个函数。
// main.c
#include <stdio.h>
#include <stdlib.h>int add(int a, int b) {return a + b;
}int sum(int *a, int num){int sum = 0;for(int i=0; i<num; i++){sum += a[i];}return sum;
}
- 将
main.c
编译为动态链接库mainlib.dll
gcc -shared -o mainlib.dll main.c
- 现在我们的文件夹下便会多出一个
mainlib.dll
库文件,接下来我们在python中调用并且使用它。
# demo.py
import ctypes
from ctypes import *
mainlib = ctypes.CDLL('test/mainlib.dll')a = ctypes.c_int(1)
b = ctypes.c_int(2)
print(mainlib.add(a,b))# 要想传入int类型的数组,就必须按照下面的方式先进行定义
int_array = (c_int * 3)(1, 2, 3)
num = ctypes.c_int(3)
print(mainlib.sum(int_array, 3))
总结: ctypes可以应用到在python中调用c函数,也就是python调用C,也就是扩展python。
pybind11
pybind11之前我使用过,当时的场景是:有一个深度学习算子是用c++和cuda写的,要把它接入到pytorch中,相当于是python中调用c++。当时的解决方案是:使用pybind11这个工具将这个算子封装成动态库文件,然后在python端进行加载运行。
在这里,我可以很明确的告诉大家:pybind11可以使我们在python中调用C++(这是pybind11的主要目的和应用),也可以使我们在C++中调用python。 下面给出两个示例。
在python中调用C++
- 安装pybind11
这里我建议使用conda install
的方式安装pybind11,否在后面在C++中会找不到pybind的头文件等。
conda install pybind11
- 创建main.cpp
#include <pybind11/pybind11.h>namespace py = pybind11;int add(int i, int j){return i+j;
}PYBIND11_MODULE(example, m) {m.doc() = "pybind11 example plugin"; // optional module docstringm.def("add", &add, "A function that adds two numbers");
}
可以看到,在main.c
中定义了一个add函数。后面几行添加了pybind接口的代码。
- 生成动态链接库
生成动态链接库这一部分,很多教程中使用的都是一个setup.py
,这里我使用从官网得到的命令行生成.so
文件。
c++ -O3 -Wall -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes) main.cpp -o example$(python3-config --extension-suffix)
运行完这条指令后,可以看到文件夹中多了一个以example开头的.so
文件。
- 在python中调用该动态链接库
import example
example.add(1,2) # 3
以上就是使用pybind11在python中调用c++的全流程。
在C++中调用python
这一部分互联网上资源很少,我没有找到一个完整的demo,最后从pybind11的官网 找到了一些demo,这里进行展示。
- 准备c++环境
因为我是使用cmake编译代码,所以第一步要找到pybind11的头文件,也就是确保CmakeLists.txt文件正确。下面是我的cmake文件。
cmake_minimum_required(VERSION 3.16)
project(main)find_package(pybind11 REQUIRED) # 寻找pybind11add_executable(main main.cpp)
target_link_libraries(main pybind11::embed)
- demo
#include <pybind11/embed.h> // 注意,这里的头文件和上一个不一样
#include <iostream>
namespace py = pybind11;int main(){py::scoped_interpreter guard{}; // 激活python解释器py::print("hello, world!"); // 使用python apipy::exec(R"(kwargs = dict(name="World", number=42)message = "Hello, {name}! The answer is {number}".format(**kwargs)print(message))"); // 使用exec在c++中运行python代码// 在c++中导入python的模块py::module_ sys = py::module_::import("sys");py::print(sys.attr("path")); // 为了简单起见,现在的工作目录已经被添加到了`sys.path`里面。/* 1. 创建calc.py**"""calc.py located in the working directory"""* def add(i, j):* return i + j*/// 2. import calc modulepy::module_ calc = py::module_::import("calc");// 3. call calc module's methodpy::object result = calc.attr("add")(1,2);int n = result.cast<int>();std::cout<<"n = "<<n<<std::endl;return 0;
}
Cython
这里先强调一点:Cython和CPython是完全不同的两个东西以及这篇文章。
Cython是一门结合了C和Python的编程语言(Cython是python的超集),接下来我们给出Cython几种不同的作用,但是无论如何,在linux下Cython最后都会生成一个.so
文件。
加快python速度
我们有一个python写的斐波那契数列,但是运行速度太慢,因为Cython中有C语言的特性,所以我们可以使用Cython语言重写斐波那契数列,然后编译为动态链接库,然后在python代码中使用。
代码如下:
1.斐波那契数列原始的python代码:
## fib.py
def fib(n):a, b = 0.0, 1.0for i in range(n):a, b = a + b, areturn a
用Cython重写的斐波那契数列,文件后缀名为.pyx
:
## fib.pyx
def fib(int n):cdef int icdef double a = 0.0, b = 1.0for i in range(n):a, b = a + b, areturn a
- 编译
fib.pyx
文件为动态链接库.so
这里有两种编译方式,一种是使用setup.py
自动进行编译,一种是手动进行编译。-
setup.py文件
from distutils.core import setup from Cython.Build import cythonizesetup(ext_modules = cythonize("fib.pyx"))
然后在命令行运行
python setup.py build_ext --inplace
便会在同级目录下生成一个以fib
开头的动态链接库以及一个fib.c
文件,这个fib.c
文件就是fib.pyx
完全转为c
代码后结果。 -
手动编译
- 第一步:在命令行运行
cython fib.pyx
,会生成fib.c
- 使用gcc对
fib.c
编译生成动态链接库:gcc -fPIC -shared -I ~/miniconda3/include/python3.11/ fib.c -o fib.so
。注意这里python include的路径需要你自己更换为自己环境的路径。
- 第一步:在命令行运行
-
这样在第2步,我们就生成了动态链接库.so
文件。
-
在python代码中使用这个动态链接库
import fibprint(fib.fib(100))
以上就是Cython工作的大体流程。这里要注意的是:我的介绍只是一点点入门知识,Cython还是很博大精深的。
在C中调用python代码
上面我们已经说过,Cython是python的超集,所以如果我们有一个python脚本或者模块,想要在C语言环境中调用它,那么可以使用cython对这个py
文件进行编译生成动态链接库,然后在C语言中调用它即可。
注:这一种方式博主没有亲自测试过
调用Python的原生C API
这是最暴力的一种方法,我们知道,python这个语言也有C的API,所以我们可以直接在C语言代码中使用这些API来调用python模块,下面是一个简单的示例。
-
我们拥有的
my_modules.py
文件# # my_modules.py def add(a, b):print(a + b)return a + bdef helloworld(s):print("hello " + s)class A:def __init__(self, a, b) -> None:self.first = aself.second = bdef add(self):print(self.first+self.second)return "hello world"
可以看到,有两个函数(一个做求和,一个输出"hello world")和一个类。
-
构建C++的环境
我是使用cmake进行编译程序的,所以要配置好CMakeLists.txt,配置如下:
cmake_minimum_required(VERSION 3.16)
project(CallPython)find_package (Python COMPONENTS Interpreter Development) # 找到python解释器
message(STATUS "Python_VERSION: ${Python_INCLUDE_DIRS}")
message(STATUS "python_LIBRARIES: ${Python_LIBRARIES}")
# message(STATUS "python_Interpreter: ${ython_LIBRARIES}")
# message(STATUS "python_LIBRARIES: ${ython_LIBRARIES}")
include_directories(${Python_INCLUDE_DIRS} )
# 生成目标文件
add_executable(call_python call_python.cpp)
# 链接库
target_link_libraries(call_python ${Python_LIBRARIES})
- 创建
call_python.cpp
文件,文件内容如下:
#include <iostream>
#include <Python.h> // 必须要有这个头文件,在cmake中进行配置也是为了找到这个头文件int main(int argc, char** argv){// 运行Python解释器Py_Initialize();// 添加.py的路径PyRun_SimpleString("import sys");PyRun_SimpleString("sys.path.append('/home/wjq/workspace/test1')"); // py文件的父目录/*********************************** add函数 ************************************/// 导入模块PyObject* pModule = PyImport_ImportModule("my_modules"); // 导入要运行的函数PyObject* pFunc = PyObject_GetAttrString(pModule, "add");// 构造传入参数 PyObject* args = PyTuple_New(2);PyTuple_SetItem(args, 0, Py_BuildValue("i", 1));PyTuple_SetItem(args, 1, Py_BuildValue("i", 10));// 运行函数,并获取返回值PyObject* pRet = PyObject_CallObject(pFunc, args); if (pRet){long result = PyLong_AsLong(pRet); // 将返回值转换成long型 std::cout << "result:" << result << std::endl ;} /******************************** helloworld函数 *******************************/// 导入函数pFunc = PyObject_GetAttrString(pModule, "helloworld");// 构造传入参数PyObject* str = Py_BuildValue("(s)", "python");// 执行函数PyObject_CallObject(pFunc, str); /*************************** ******class A测试*******************************/PyObject* pDict = PyModule_GetDict(pModule);// 类PyObject* pClassA = PyDict_GetItemString(pDict, "A");// 类的构造对象PyObject* pConstruct = PyInstanceMethod_New(pClassA);// 类的对象PyObject * pInsA = PyObject_CallObject(pConstruct, args);// 调用类的方法PyObject* result = PyObject_CallMethod(pInsA, "add", nullptr);// 对结果进行解读if(result != nullptr){char * str_result;PyArg_Parse(result, "s", &str_result);printf("Result: %s\n", str_result); Py_DECREF(result);}// 终止Python解释器Py_Finalize(); }
结果如下所示:
11
result:11
hello python
11
Result: hello world
Python的C API有很多,这里我们只是用了几个,关于更多的API,请参考官网。
参考链接
- https://www.52txr.cn/2023/CPytonCython.html
- https://www.cnblogs.com/traditional/p/13196509.html
- https://chend0316.github.io/backend/cython/#%E7%AC%AC1%E7%AB%A0-cython%E7%9A%84%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8
- https://blog.csdn.net/u011722929/article/details/114871365
- https://www.hbblog.cn/python%26C%2B%2B/python%E5%92%8CC%E7%9A%84%E4%BA%A4%E4%BA%92/#31-pythonapi
- https://zhuanlan.zhihu.com/p/79896193
- https://blog.csdn.net/qq_42688495/article/details/120563844