python bind_all_ubuntu 16.04 上使用pybind11进行C++和Python代码相互调用

本文介绍了在Ubuntu 16.04上利用pybind11库将C++和Python代码进行交互的步骤。首先,安装必要的依赖,如pybind11和pytest。然后,通过编译和安装pybind11源码,将库文件安装到/usr/local。最后,给出了如何在CMakeLists.txt中配置和使用pybind11的示例。
摘要由CSDN通过智能技术生成

Interfacing C++ and Python with pybind11 on ubuntu 16.04

Series

Guide

requirements:

pybind11 v2.3.dev0

python 3.5

install pytest

pip3 install pytest

compile

git clone https://github.com/pybind/pybind11.git

cd pybind11

mkdir build

cd build

cmake-gui ..

with options

PYBIND11_CPP_STANDARD /std:c++11 # default c++14

PYTHON_EXECUTABLE /usr/bin/python3.5

CMAKE_INSTALL_PREFIX /usr/local

install

make and install

make -j8

sudo make install

install to /usr/local/include/pybind11 with only include and /usr/local/share/cmake/pybind11

output

Install the project...

-- Install configuration: "MinSizeRel"

-- Installing: /usr/local/include/pybind11

-- Installing: /usr/local/include/pybind11/chrono.h

-- Installing: /usr/local/include/pybind11/eigen.h

-- Installing: /usr/local/include/pybind11/stl.h

-- Installing: /usr/local/include/pybind11/complex.h

-- Installing: /usr/local/include/pybind11/detail

-- Installing: /usr/local/include/pybind11/detail/internals.h

-- Installing: /usr/local/include/pybind11/detail/common.h

-- Installing: /usr/local/include/pybind11/detail/descr.h

-- Installing: /usr/local/include/pybind11/detail/init.h

-- Installing: /usr/local/include/pybind11/detail/class.h

-- Installing: /usr/local/include/pybind11/detail/typeid.h

-- Installing: /usr/local/include/pybind11/common.h

-- Installing: /usr/local/include/pybind11/iostream.h

-- Installing: /usr/local/include/pybind11/buffer_info.h

-- Installing: /usr/local/include/pybind11/attr.h

-- Installing: /usr/local/include/pybind11/numpy.h

-- Installing: /usr/local/include/pybind11/pybind11.h

-- Installing: /usr/local/include/pybind11/operators.h

-- Installing: /usr/local/include/pybind11/options.h

-- Installing: /usr/local/include/pybind11/cast.h

-- Installing: /usr/local/include/pybind11/eval.h

-- Installing: /usr/local/include/pybind11/embed.h

-- Installing: /usr/local/include/pybind11/pytypes.h

-- Installing: /usr/local/include/pybind11/functional.h

-- Installing: /usr/local/include/pybind11/stl_bind.h

-- Installing: /usr/local/share/cmake/pybind11/pybind11Config.cmake

-- Installing: /usr/local/share/cmake/pybind11/pybind11ConfigVersion.cmake

-- Installing: /usr/local/share/cmake/pybind11/FindPythonLibsNew.cmake

-- Installing: /usr/local/share/cmake/pybind11/pybind11Tools.cmake

-- Installing: /usr/local/share/cmake/pybind11/pybind11Targets.cmake

Usage

pybind11

CMakeLists.txt

find_package(pybind11 CONFIG REQUIRED)

include_directories(${pybind11_INCLUDE_DIRS})

MESSAGE( [MAIN] "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}")

MESSAGE( [Main] " pybind11_INCLUDE_DIRS = ${pybind11_INCLUDE_DIRS}")

MESSAGE( [Main] " pybind11_LIBRARIES = ${pybind11_LIBRARIES}")

add_library(examplelib

${HEADER_FILES}

${SOURCE_FILES}

)

target_link_libraries (examplelib

pybind11::module

${xxx_LIBRARIES}

)

embed

CMakeLists.txt

find_package(pybind11 CONFIG REQUIRED)

include_directories(${pybind11_INCLUDE_DIRS})

MESSAGE( [MAIN] "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}")

MESSAGE( [Main] " pybind11_INCLUDE_DIRS = ${pybind11_INCLUDE_DIRS}")

MESSAGE( [Main] " pybind11_LIBRARIES = ${pybind11_LIBRARIES}")

add_executable(cpp_use_python cpp_use_python.cpp)

target_link_libraries(cpp_use_python PRIVATE pybind11::embed)

Reference

History

20181127: created.

Copyright

Post author: kezunlin

Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值