• ubuntu 16.04 上使用pybind11进行C++和Python代码相互调用 | Interfacing C++ and Python with pybind11 on ubuntu 16.04


    本文首发于个人博客https://kezunlin.me/post/a41adc1/,欢迎阅读!

    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

  • 相关阅读:
    14款经典的MySQL客户端软件
    淘宝、百度软件工程师们小调皮,各种霸气外漏
    使用Navicat for Oracle工具连接oracle出错:ORA-12737
    解析xlsx与xls--使用2012poi.jar
    LUOGU P4163 [SCOI2007]排列
    LUOGU P1337 [JSOI2004]平衡点 / 吊打XXX(模拟退火)
    LUOGU P1438 无聊的数列 (差分+线段树)
    LUOGU P1967 货车运输(最大生成树+树剖+线段树)
    LUOGU P3382 【模板】三分法 (三分)
    LUOGU P4027 [NOI2007]货币兑换 (斜率优化+CDQ分治)
  • 原文地址:https://www.cnblogs.com/kezunlin/p/11841494.html
Copyright © 2020-2023  润新知