ubuntu - pybind11的使用

文章目录

  • 1.安装
  • 2.例子
  • 错误
  • reference

1.安装

首先安装pytest
pip3 install pytest
1.1.编译
git clone https://github.com/pybind/pybind11.git
cd pybind11
mkdir build
cd build
cmake …

cmake的参数:

cmake -DPYTHON_LIBRARY=/Users/zhangxin/anaconda3/envs/py36_pytorch041/lib/libpython3.6m.dylib
-DPYTHON_INCLUDE_DIR=/Users/zhangxin/anaconda3/envs/py36_pytorch041/include/python3.6
-DPYTHON_EXECUTABLE=/Users/zhangxin/anaconda3/envs/py36_pytorch041/bin/python3.6

1.2.安装
make -j8
sudo make install
确定线程个数,然后安装

2.例子

example.cpp的内容:

#include 
namespace py = pybind11;

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

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;
}

CMakeLists.txt的内容:

cmake_minimum_required(VERSION 3.5.1)
project(example) 
add_subdirectory(pybind11)
pybind11_add_module(example example.cpp)
SET( CMAKE_CXX_FLAGS "-std=c++11 -O3")


目录下的内容如下:
ubuntu - pybind11的使用_第1张图片
编译生成过程:
a. cmake .

hlx2@NLP:~/make/a$ cmake .
– The C compiler identification is GNU 5.4.0
– The CXX compiler identification is GNU 5.4.0
– Check for working C compiler: /usr/bin/cc
– Check for working C compiler: /usr/bin/cc – works
– Detecting C compiler ABI info
– Detecting C compiler ABI info - done
– Detecting C compile features
– Detecting C compile features - done
– Check for working CXX compiler: /usr/bin/c++
– Check for working CXX compiler: /usr/bin/c++ – works
– Detecting CXX compiler ABI info
– Detecting CXX compiler ABI info - done
– Detecting CXX compile features
– Detecting CXX compile features - done
– Found PythonInterp: /usr/local/bin/python3.7 (found version “3.7.1”)
– Found PythonLibs: /usr/local/lib/libpython3.7m.a
– pybind11 v2.5.dev1
– Performing Test HAS_FLTO
– Performing Test HAS_FLTO - Success
– LTO enabled
– Configuring done
– Generating done
– Build files have been written to: /home/hlx2/make/a
ubuntu - pybind11的使用_第2张图片

b. make

hlx2@NLP:~/make/a$ make
Scanning dependencies of target example
[ 50%] Building CXX object CMakeFiles/example.dir/example.cpp.o
[100%] Linking CXX shared module example.cpython-37m-x86_64-linux-gnu.so
[100%] Built target example

ubuntu - pybind11的使用_第3张图片

错误

如果CMakeLists.txt没有SET( CMAKE_CXX_FLAGS “-std=c++11 -O3”),则会出现一下错误:


/home/hlx2/make/a/pybind11/include/pybind11/detail/internals.h: At global scope:
/home/hlx2/make/a/pybind11/include/pybind11/detail/internals.h:215:38: error: variable or field ‘translate_exception’ declared void
inline void translate_exception(std::exception_ptr p) {
^
/home/hlx2/make/a/pybind11/include/pybind11/detail/internals.h:215:33: error: ‘exception_ptr’ is not a member of ‘std’
inline void translate_exception(std::exception_ptr p) {
^
In file included from /home/hlx2/make/a/example.cpp:1:0:
/home/hlx2/make/a/pybind11/include/pybind11/pybind11.h:2193:29: error: expected ‘}’ before end of line
/home/hlx2/make/a/pybind11/include/pybind11/pybind11.h:2193:29: error: expected ‘}’ before end of line
/home/hlx2/make/a/pybind11/include/pybind11/pybind11.h:2193:29: error: expected declaration before end of line
In file included from /home/hlx2/make/a/pybind11/include/pybind11/cast.h:13:0,
from /home/hlx2/make/a/pybind11/include/pybind11/attr.h:13,
from /home/hlx2/make/a/pybind11/include/pybind11/pybind11.h:44,
from /home/hlx2/make/a/example.cpp:1:
/home/hlx2/make/a/pybind11/include/pybind11/pytypes.h:315:20: warning: inline function ‘std::__cxx11::string pybind11::detail::error_string()’ used but never defined
inline std::string error_string();
^
/home/hlx2/make/a/pybind11/include/pybind11/pytypes.h:333:12: warning: inline function ‘virtual pybind11::error_already_set::~error_already_set()’ used but never defined
inline ~error_already_set();
^
/home/hlx2/make/a/pybind11/include/pybind11/pytypes.h:333:12: warning: inline function ‘virtual pybind11::error_already_set::~error_already_set()’ used but never defined
CMakeFiles/example.dir/build.make:62: recipe for target ‘CMakeFiles/example.dir/example.cpp.o’ failed
make[2]: *** [CMakeFiles/example.dir/example.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target ‘CMakeFiles/example.dir/all’ failed
make[1]: *** [CMakeFiles/example.dir/all] Error 2
Makefile:83: recipe for target ‘all’ failed
make: *** [all] Error 2

reference

https://blog.csdn.net/sdlypyzq/article/details/102868602
https://www.cnblogs.com/findumars/p/10363399.html
http://www.111com.net/phper/167239.htm

你可能感兴趣的:(Python)