pybind11教程翻译

pybind11对应教程
First Step
This sections demonstrates the basic features of pybind11. Before getting started, make sure that development environment is set up to compile the included set of test cases.
开始之前,确保你的环境设置成为能够编译测试集的开发环境。
Compiling the test cases
Linux/macOS
On Linux you’ll need to install the python-dev or python3-dev packages as well as cmake.On macOS,the included python version works out of the box,but cmake must still be installed.
需要提前安装对应的python-dev库和python3-dev库
After installing the prerequisites,run

mkdir build
cd build
cmake ..
make check -j 4

The last line will both compile and run the tests.
(运行上述的命令)
这里我的cmake …命令暂时没有运行成功
错误提示查阅网上的资料说这里安装的mysql数据库对应的版本不对,但是实际上这里我并没有安装对应的mysql数据库,所以这个部分的错误先跳过,继续往下进行。
Header and namespace conventions
标题和命名空间的习惯
For brevity,all code examples assume that the following two lines are present:
所有对应的代码,下面两行需要展示出来

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

Some features may require additional headers,but those will be specified as needed.
其他特征可能需要额外的标题,但是这上面的内容必须指明。
Creating bindings for a simple function
Let’s start by creating Python bindings for an extremely simple function,which adds two numbers and returns their result:

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

For simplicity,we’ll put both this function and the binding code into a file named example.cpp with the following contents:

#include <pybind11/pybind11.h>
int add(int i, int j) {
    return i + j;
}
PYBIND11_MODULE(example, m) {
    m.doc() = "pybind11 example plugin"; // optional module docstring
    m.def("add", &add, "A function which adds two numbers");
}

注释1:In practice,implementation and binding code will generally be located in separate files.
The PYBIND11_MODULE() macro creates a function that will be called when an import statement is issued from within Python.
PYBIND11_MODULE宏定义了一个函数,当调用import的时候这个函数就会被Python的内部引用。The module name(example) is given as the first macro argument(it should not be in quotes).(example)宏变量作为第一个宏变量参数(它不应该被引用)
The second argument(m) defines a variable of type py::module_ which is the main interface for creating bindings.
m定义了一个变量py::module作为创建联系的主要函数。
The method module_::def() generates binding code that exposes the add() function to Python.
module_::def()产生绑定函数将add函数暴露给Python
Note
Notice how little code was needed to expose our function to Python:all details regarding the function’s parameters and return value were automatically inferred using template metaprogramming.
注意多么简短将我们的函数暴露给Python,所以关于函数的细节以及返回值都自动产生使用metaprogramming模板。
This overall approach and the used syntax are borrowed from Boost.Python,though the underlying implementation is very different.
总体的方法和使用的语法从Boost.Python中借鉴,尽管隐藏的实现非常的不同。
pybind11 is a header-only library,hence it is not necessary to link against any special libraries and there are no intermediate(magic) translation steps.On Linux,the above example can be compiled using the following command.
pybind11是一个只有头的库,不必要去连接任何特殊的库,没有直接的翻译步骤。在Linux上面,上述例子可以被使用下面的命令编译出来:

$ c++ -O3 -Wall -shared -std=c++11 -fPIC $(python3 -m pybind11 --includes) example.cpp -o example$(python3-config --extension-suffix)

header only library:把一个库的内容完全写在头文件中,不带任何cpp文件。这是一个巧合,决不是C++的原始设计。
Note
If you used include as a submodule to get the pybind11 source,then use

$(python3-config --includes) -Iextern/pybind11/include

instead of

$(python3 -m pybind11 --includes)

in the above compilation,as explained in Building manually.
For more details on the required compiler flags on Linux and macOS,see Building manually.For complete cross-platform compilation instructions,refer to the Build system page.
The python_example and cmake_example repositories are also a good place to start. They are both complete project examples with cross-platform build systems. The only difference between the two is that python_example uses Python’s setuptools to build the module, while cmake_example uses CMake (which may be preferable for existing C++ projects).
Building the above C++ code will produce a binary module file that can be imported to Python. Assuming that the compiled module is located in the current directory, the following interactive Python session shows how to load and execute the example:
(假设编译的模块就在当前的目录,这里编译产生了一个二进制的模块文件)
产生编译后的模块文件使用以下的操作调用对应的c++文件:

import example
example.add(1,2)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值