pybind11简单使用
生活随笔
收集整理的這篇文章主要介紹了
pybind11简单使用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
https://sanzo.top/Default/pybind11
pybind11介紹
github.com/pybind/pybind11
使用pybind11可以將C++庫(kù)綁定到Python,即在Python內(nèi)調(diào)用C++代碼,同樣也可以在C++中調(diào)用Python代碼。
編譯安裝
# install pytest pip install pytest# install pybind11 git clone https://github.com/pybind/pybind11.git cd pybind11 && mkdir build && cd build cmake .. && make && sudo make install接下來(lái)使用pybind11演示下如何實(shí)現(xiàn)C++調(diào)用Python,以及C++調(diào)用Python。
Python調(diào)用C++
#include <pybind11/pybind11.h>/***********************調(diào)用普通函數(shù)***********************/ template <typename T> T add(T a, T b) {return a + b; }PYBIND11_MODULE(pyadd, m) {m.doc() = "test for add()";m.def("add", &add<int>, "add two number.");m.def("add", &add<double>, "add two number.");m.def("add", &add<long long>, "add two number.");m.attr("__version__") = "dev";/*** >>> import pyadd* >>> pyadd.__version__* 'dev' * >>> pyadd.add(1.1, 2.2) * 3.3000000000000003 */ }/***********************調(diào)用類***********************/ namespace test_class { class Person { public:Person() {name = "Sanzo";age = 21;}Person(std::string name, int age) {this->name = name;this->age = age;}std::string getName() {return this->name;}void setName(std::string name) {this->name = name;}int getAge() {return age;}std::string name;private:int age; }; }PYBIND11_MODULE(test_class, m) {m.doc() = "test_class::person";pybind11::class_<test_class::Person>(m, "Person").def(pybind11::init()).def(pybind11::init<std::string, int>()).def("getAge", &test_class::Person::getAge).def("getName", &test_class::Person::getName).def("setName", &test_class::Person::setName).def_readwrite("name", &test_class::Person::name);/*>>> a = test_class.Person()>>> a.getName()'Sanzo'>>> a.getAge()21>>> a.setName("Sanzo00")>>> a.getName()'Sanzo00'>>> b = test_class.Person("Sazo00", 21)>>> b.getName()'Sazo00'>>> b.getAge()21 */ }/***********************調(diào)用stl***********************/ #include <pybind11/stl.h>void printV(std::vector<int> &v) {for (auto &it : v) {std::cout << it << " ";}std::cout << std::endl; }PYBIND11_MODULE(stl, m) {m.doc() = "test for stl.";m.def("printV", &printV);/*>>> import stl>>> stl.printV([1, 2, 3, 4])1 2 3 4 */ } // file name: CMakeLists.txt cmake_minimum_required(VERSION 3.5) project(test_pybind) add_subdirectory(pybind11) pybind11_add_module(pyadd py_call_cpp.cc) pybind11_add_module(test_class py_call_cpp.cc) pybind11_add_module(stl py_call_cpp.cc)C++調(diào)用Python
// file name: cpp_call_py.ccc #include <iostream> #include <pybind11/pybind11.h> #include <pybind11/embed.h>namespace py = pybind11;int main() {py::scoped_interpreter python;py::module t = py::module::import("test_add");auto result = t.attr("add")(1, 2);int sum = result.cast<int>();std::cout << "sum = " << sum << std::endl;return 0; }/*vim ~/.bashrc # 將工作目錄添加到環(huán)境變量中export PYTHONPATH=/home/ubuntu/test_pybind:$PATH */ // file name: test_add.py def add(a, b):print(f"add({a}, {b})")return a + b // file name: CMakeLists.txt cmake_minimum_required(VERSION 3.5) project(test_pybind) add_subdirectory(pybind11)add_executable(cpp_call_py cpp_call_py.cc test_add.py)target_include_directories(cpp_call_pyPUBLIC/usr/include/python3.8 )target_link_libraries(cpp_call_pyPUBLIC/home/ubuntu/miniconda3/lib/libpython3.8.so )注意
總結(jié)
以上是生活随笔為你收集整理的pybind11简单使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Gemini论文笔记
- 下一篇: 树莓派小车(远程控制、PWM变速、超声波