Install

#pre-requirements
sudo apt install python3-dev
pip install pytest 
#download source code
git clone https://github.com/pybind/pybind11.git
cd pybind11
mkdir build
cd build
#make 
cmake ..
make check -j 8
sudo make install

Using

Attention: DO NOT use g++ or c++ to build (in case we haven’t install pybind11 in python environment(which is also no need to do)).

Instead, we use Cmake to compile our code.

cmake_minimum_required(VERSION 3.0)

set(PROJ_NAME "example")
project(${PROJ_NAME})

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3")

find_package(pybind11 REQUIRED)
pybind11_add_module(${PROJ_NAME} example.cpp)

target_link_libraries(${PROJ_NAME} PRIVATE pybind11::module)

find_package(Python)
find_path(PYTHON_SITE_PACKAGES site-packages ${PYTHON_INCLUDE_PATH}/..)

install(TARGETS ${PROJ_NAME} RUNTIME DESTINATION ${PYTHON_SITE_PACKAGES}
							 LIBRARY DESTINATION ${PYTHON_SITE_PACKAGES}
							 ARCHIVE DESTINATION ${PYTHON_SITE_PACKAGES}
							 )

And folder is such like:

├── build
│   ├  ...
├── CMakeLists.txt
└── example.cpp