python調用C++之pybind11入門(相互調用)

python調用C/C++有很多的方法,如boost.python, swig, ctypes, pybind11等,這些方法有繁有簡,而pybind11的優勢是對C++ 11支持很好,API比較簡單,如今咱們就簡單記下Pybind11的入門操做。python

1. pybind11簡介與環境安裝
pybind11是一個輕量級的只包含頭文件的庫,它主要是用來在已有的 C++代碼的基礎上作擴展,它的語法和目標很是像Boost.Python,但Boost.Python爲了兼容現有的基本全部的C++編譯器而變得很是複雜和龐大,而所以付出的代價是不少晦澀的模板技巧以及不少沒必要要的對舊版編譯器的支持。Pybind11摒棄了這些支持,它只支持python2.7以上以及C++ 11以上的編譯器,使得它比Boost.Python更加簡潔高效。
爲了使用pybind11,咱們須要支持C++ 11標準的編譯器(GCC 4.8以上,VS 2015 Update 3以上)以及python 2.7以上的版本,還須要下載CMake,有了這些之後,
1. 首先,咱們從 pybind11 github網址:https://github.com/pybind/pybind11 上下載源碼。
2. cmake工程以前,要先安裝pytest pip install pytest,不然會出錯
3. 用CMake編譯並運行測試用例:ios

mkdir build
cd build
cmake ..
cmake --build . --config Release --target check
1
2
3
4
若是全部測試用例都經過了,說明安裝成功了。git

2. python調用C++
下載編譯好pybind11以後,咱們就能夠開始對着官方的pybind11 Tutorial進行學習了,詳細的入門教程及語法請參考官方文檔,這裏,咱們簡單演示下如何編寫供python調用的C++模塊.
首先,咱們編寫一個C++源文件,命名爲example.cppgithub

#include <pybind11/pybind11.h>
namespace py = pybind11;windows

int add(int i, int j)
{
return i + j;
}python2.7

PYBIND11_MODULE(example, m)
{
// optional module docstring
m.doc() = "pybind11 example plugin";
// expose add function, and add keyword arguments and default arguments
m.def("add", &add, "A function which adds two numbers", py::arg("i")=1, py::arg("j")=2);工具

// exporting variables
m.attr("the_answer") = 42;
py::object world = py::cast("World");
m.attr("what") = world;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2.1 編譯
而後,在windows下使用工具vs2015 x86 Native Tools Command Prompt (由於個人python是32位版本,若是是64位版本的,請使用vs2015 x64 Native Tools Command Prompt)進行編譯: 學習

cl example.cpp /I "H:/Allfiles/pybind11/include" /I "C:/Python27/include" /LD /Fe:example.pyd /link/LIBPATH:"C:/python27/libs/" 測試

編譯成功後,會在example.cpp相同目錄下生成example.pyd文件,這個就是python能夠直接導入的庫,運行:ui

import example
example.add(3, 4)

[out]: 7
1
2
3
4
有了編譯成功的模塊,即可以使用我在另外一篇博客Python模塊搜索路徑中提到的方法,將其用.pth或者PYTHONPATH的方法加入到python搜索路徑,之後在咱們本身的環境中就能夠隨時隨地使用這個模塊啦。

2.2 CMake的編譯方法
固然,咱們也可使用CMake進行編譯。首先寫一個CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)
project(example)

add_subdirectory(pybind11)
pybind11_add_module(example example.cpp)
1
2
3
4
5
這裏要求example.cpp放在和pybind11同一級的目錄下,而後CMake,便會生成一個vs 2015的工程文件,用vs打開工程文件進行build,就能夠生成example.pyd了。

3. C++調用python
使用pybind11,也很容易從C++裏調用python腳本:
首先,咱們用vs 2015新建一個工程,而且將Python的包含目錄和庫目錄,以及pybind11的包含目錄配置到工程,個人配置以下:


而後,新建一個源文件example.cpp:

#include <pybind11/embed.h>
#include <iostream>

namespace py = pybind11;

int main() {
py::scoped_interpreter python;

//py::module sys = py::module::import("sys");
//py::print(sys.attr("path"));

py::module t = py::module::import("example");
t.attr("add")(1, 2);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
最後,在工程目錄下加入腳本example.py:

def add(i, j):
print("hello, pybind11")
return i+j
1
2
3
運行工程,獲得以下的結果:

運行成功!!!

總結
本文中咱們簡單介紹了怎麼使用pybind11進行python和C++的相互調用,這些只是入門級別的例子,可是能夠work了,這很重要。深刻進行研究使用,仍是等之後用到再說吧。

參考文獻pybind11 githubpybind11 TutorialC++與Python的互操做Building and testing a hybrid Python/C++ package--------------------- 做者:fitzzhang 來源:CSDN 原文:https://blog.csdn.net/fitzzhang/article/details/78988682 版權聲明:本文爲博主原創文章,轉載請附上博文連接!

相關文章
相關標籤/搜索