Ubuntu 下 pybind11 使用环境搭建
-
安装 anaconda
-
安装 CLion
-
clone pybind11
-
build and install pybind11
-
编写 cmake
cmake_minimum_required(VERSION 3.15) project(example) set(CMAKE_CXX_STANDARD 11) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(PYTHON3_INC_DIR "/home/cugxy/anaconda3/include/python3.7m" CACHE PATH "") set(PYTHON_EXECUTABLE "/home/cugxy/anaconda3/bin/python3.7m") add_subdirectory(${CMAKE_SOURCE_DIR}/3rdpart/pybind11) pybind11_add_module(example src/example.cpp) target_include_directories(example PRIVATE ${PYTHON3_INC_DIR})
注意上文中
set(PYTHON3_INC_DIR "/home/cugxy/anaconda3/include/python3.7m" CACHE PATH "")
和set(PYTHON_EXECUTABLE "/home/cugxy/anaconda3/bin/python3.7m")
非常重要, 必须指定你要使用的 python 的位置. 否则将无法成功 -
编写 example.cpp
#include <pybind11/pybind11.h> namespace py = pybind11; int add(int i, int j) { return i + j; } PYBIND11_MODULE(example, m) { m.doc() = "Hello world!"; m.def("add", &add); }