pybind11的处理过程
我首先安装了pybind,参考了许多教程,但很多教程的貌似都来自微软导出pyd的方法,但是实际上linux下导出so文件而非pyd文件,后续运行的实例来自https://github.com/tdegeus/pybind11_examples(代码就不再本文中介绍了),因为python版本问题一直无法成功运行,所以我按照github的结构重新配置了文件(即下载pybind11源文件到当前目录,使用非安装方式,而无需再Cmakelists里进行任何修改),但仍然无法正常运行。后来看有说是python版本问题导致能够导出so却无法运行,所以后续就尝试了几种更改python版本的方式,运行成功。
pybind 安装
get
(base) pdd@pdd-Dell-G15-5511:~/CLionProjects$ git clone https://github.com/pybind/pybind11
正克隆到 'pybind11'...
remote: Enumerating objects: 25971, done.
remote: Counting objects: 100% (183/183), done.
remote: Compressing objects: 100% (113/113), done.
remote: Total 25971 (delta 80), reused 117 (delta 62), pack-reused 25788
接收对象中: 100% (25971/25971), 10.04 MiB | 739.00 KiB/s, 完成.
处理 delta 中: 100% (18198/18198), 完成.
make
(base) pdd@pdd-Dell-G15-5511:~/CLionProjects/pybind11$ mkdir build && cd build && cmake ..
-- The CXX compiler identification is GNU 9.5.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- pybind11 v2.11.0 dev1
-- CMake 3.22.1
-- Found PythonInterp: /home/pdd/anaconda3/bin/python (found suitable version "3.9.13", minimum required is "3.6")
-- Found PythonLibs: /home/pdd/anaconda3/lib/libpython3.9.so
-- PYTHON 3.9.13
-- Performing Test HAS_FLTO
-- Performing Test HAS_FLTO - Success
-- pybind11::lto enabled
-- pybind11::thin_lto enabled
-- Setting tests build type to MinSizeRel as none was specified
-- Building tests WITHOUT Eigen, use -DDOWNLOAD_EIGEN=ON on CMake 3.11+ to download
-- Could NOT find Boost (missing: Boost_INCLUDE_DIR) (Required is at least version "1.56")
-- Found pytest 7.1.2
-- Catch not detected. Interpreter tests will be skipped. Install Catch headers manually or use `cmake -DDOWNLOAD_CATCH=ON` to fetch them automatically.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pdd/CLionProjects/pybind11/build
错误提示与解决
error | solve |
---|---|
Could NOT find Boost (missing: Boost_INCLUDE_DIR) (Required is at least version “1.56”) | Linux平台的boost安装 |
Catch not detected. Interpreter tests will be skipped. Install Catch headers manually or use cmake -DDOWNLOAD_CATCH=ON to fetch them automatically. | 同样使用下行的命令解决 |
Building tests WITHOUT Eigen, use -DDOWNLOAD_EIGEN=ON on CMake 3.11+ to download | build$cmake -DDOWNLOAD_EIGEN=ON -DDOWNLOAD_CATCH=ON -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") .. # https://pybind11.readthedocs.io/en/stable/compiling.html#building-with-cmake |
- 命令执行过程:
(yolocopy) pdd@pdd-Dell-G15-5511:~/CLionProjects/pybind11/build$ cmake -DDOWNLOAD_EIGEN=ON -DDOWNLOAD_CATCH=ON -DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") ..
-- pybind11 v2.11.0 dev1
-- CMake 3.22.1
-- Found PythonInterp: /home/pdd/anaconda3/envs/yolocopy/bin/python3 (found suitable version "3.7.12", minimum required is "3.6")
-- Found PythonLibs: /home/pdd/anaconda3/lib/libpython3.9.so
-- PYTHON 3.7.12
-- pybind11::lto enabled
-- pybind11::thin_lto enabled
-- Downloading Eigen 3.4.0 (929bc0e191d0927b1735b9a1ddc0e8b77e3a25ec) from https://gitlab.com/libeigen/eigen.git
-- Building tests with Eigen v3.4.0
-- Found Boost: /usr/lib/x86_64-linux-gnu/cmake/Boost-1.74.0/BoostConfig.cmake (found suitable version "1.74.0", minimum required is "1.56")
-- Downloading catch v2.13.9...
-- Building interpreter tests using Catch v2.13.9
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pdd/CLionProjects/pybind11/build
install
(base) pdd@pdd-Dell-G15-5511:~/CLionProjects/pybind11/build$ make check -j8
[ 97%] Building CXX object tests/CMakeFiles/pybind11_tests.dir/test_virtual_functions.cpp.o
[100%] Linking CXX shared module pybind11_tests.cpython-39-x86_64-linux-gnu.so
------ pybind11_tests.cpython-39-x86_64-linux-gnu.so file size: 2721000
[100%] Built target pybind11_tests
=========================================================================================================== test session starts ============================================================================================================
platform linux -- Python 3.9.13, pytest-7.1.2, pluggy-1.0.0
C++ Info: 9.5.0 C++14 __pybind11_internals_v4_gcc_libstdcpp_cxxabi1013__ PYBIND11_SIMPLE_GIL_MANAGEMENT=False
rootdir: /home/pdd/CLionProjects/pybind11/tests, configfile: pytest.ini
plugins: anyio-3.5.0
collected 655 items / 2 skipped
../../tests/test_async.py .. [ 0%]
../../tests/test_buffers.py ......... [ 1%]
../../tests/test_builtin_casters.py .....s.............. [ 4%]
../../tests/test_call_policies.py ........ [ 5%]
../../tests/test_callbacks.py .............s [ 8%]
../../tests/test_chrono.py ........................................... [ 14%]
../../tests/test_class.py ................................ [ 19%]
../../tests/test_const_name.py ...................... [ 22%]
../../tests/test_constants_and_functions.py ..... [ 23%]
../../tests/test_copy_move.py ....s... [ 24%]
../../tests/test_custom_type_casters.py ... [ 25%]
../../tests/test_custom_type_setup.py .. [ 25%]
../../tests/test_docstring_options.py . [ 25%]
../../tests/test_enum.py ......... [ 27%]
../../tests/test_eval.py .... [ 27%]
../../tests/test_exceptions.py ......................... [ 31%]
../../tests/test_factory_constructors.py ............... [ 33%]
../../tests/test_gil_scoped.py .......................................................................................... [ 47%]
../../tests/test_iostream.py ...................... [ 50%]
../../tests/test_kwargs_and_defaults.py ........ [ 52%]
../../tests/test_local_bindings.py .......... [ 53%]
../../tests/test_methods_and_attributes.py ...................... [ 57%]
../../tests/test_modules.py ....... [ 58%]
../../tests/test_multiple_inheritance.py .................. [ 60%]
../../tests/test_numpy_array.py ......................................................... [ 69%]
../../tests/test_numpy_dtypes.py ............... [ 71%]
../../tests/test_numpy_vectorize.py ........ [ 73%]
../../tests/test_opaque_types.py ... [ 73%]
../../tests/test_operator_overloading.py ..... [ 74%]
../../tests/test_pickling.py ........ [ 75%]
../../tests/test_pytypes.py .............................s........................................................ [ 88%]
../../tests/test_sequences_and_iterators.py .s............ [ 90%]
../../tests/test_smart_ptr.py ............. [ 92%]
../../tests/test_stl.py ........sss.sss........ [ 96%]
../../tests/test_stl_binders.py .......... [ 97%]
../../tests/test_tagbased_polymorphic.py . [ 98%]
../../tests/test_thread.py .. [ 98%]
../../tests/test_union.py . [ 98%]
../../tests/test_virtual_functions.py .......... [100%]
========================================================================================================= short test summary info ==========================================================================================================
SKIPPED [1] ../../tests/test_eigen_matrix.py:6: could not import 'pybind11_tests.eigen_matrix': No module named 'pybind11_tests.eigen_matrix'; 'pybind11_tests' is not a package
SKIPPED [1] ../../tests/test_eigen_tensor.py:6: could not import 'pybind11_tests.eigen_tensor': No module named 'pybind11_tests.eigen_tensor'; 'pybind11_tests' is not a package
SKIPPED [1] ../../tests/test_builtin_casters.py:145: no <string_view>
SKIPPED [1] ../../tests/test_callbacks.py:213: Current PYBIND11_INTERNALS_VERSION too low
SKIPPED [1] ../../tests/test_copy_move.py:77: no <optional>
SKIPPED [1] ../../tests/test_pytypes.py:403: Not defined: PYBIND11_HANDLE_REF_DEBUG
SKIPPED [1] ../../tests/test_sequences_and_iterators.py:13: no <optional>
SKIPPED [1] ../../tests/test_stl.py:112: no <optional>
SKIPPED [1] ../../tests/test_stl.py:144: no <experimental/optional>
SKIPPED [1] ../../tests/test_stl.py:176: no <boost/optional>
SKIPPED [1] ../../tests/test_stl.py:235: no <filesystem>
SKIPPED [1] ../../tests/test_stl.py:254: no <variant>
SKIPPED [1] ../../tests/test_stl.py:271: no std::monostate
===================================================================================================== 644 passed, 13 skipped in 11.69s =====================================================================================================
[100%] Built target pytest
[100%] Built target check
(base) pdd@pdd-Dell-G15-5511:~/CLionProjects/pybind11/build$ sudo make install
[sudo] pdd 的密码:
[ 87%] Built target pybind11_tests
[ 91%] Built target pybind11_cross_module_tests
[ 95%] Built target cross_module_interleaved_error_already_set
[100%] Built target cross_module_gil_utils
Install the project...
-- Install configuration: "MinSizeRel"
-- Installing: /usr/local/include/pybind11
-- Installing: /usr/local/include/pybind11/iostream.h
-- Installing: /usr/local/include/pybind11/stl.h
-- Installing: /usr/local/include/pybind11/stl_bind.h
-- Installing: /usr/local/include/pybind11/operators.h
-- Installing: /usr/local/include/pybind11/pybind11.h
-- Installing: /usr/local/include/pybind11/cast.h
-- Installing: /usr/local/include/pybind11/eigen
-- Installing: /usr/local/include/pybind11/eigen/tensor.h
-- Installing: /usr/local/include/pybind11/eigen/matrix.h
-- Installing: /usr/local/include/pybind11/eigen.h
-- Installing: /usr/local/include/pybind11/functional.h
-- Installing: /usr/local/include/pybind11/stl
-- Installing: /usr/local/include/pybind11/stl/filesystem.h
-- Installing: /usr/local/include/pybind11/attr.h
-- Installing: /usr/local/include/pybind11/common.h
-- Installing: /usr/local/include/pybind11/eval.h
-- Installing: /usr/local/include/pybind11/detail
-- Installing: /usr/local/include/pybind11/detail/descr.h
-- Installing: /usr/local/include/pybind11/detail/class.h
-- Installing: /usr/local/include/pybind11/detail/typeid.h
-- Installing: /usr/local/include/pybind11/detail/common.h
-- Installing: /usr/local/include/pybind11/detail/init.h
-- Installing: /usr/local/include/pybind11/detail/type_caster_base.h
-- Installing: /usr/local/include/pybind11/detail/internals.h
-- Installing: /usr/local/include/pybind11/options.h
-- Installing: /usr/local/include/pybind11/pytypes.h
-- Installing: /usr/local/include/pybind11/buffer_info.h
-- Installing: /usr/local/include/pybind11/gil.h
-- Installing: /usr/local/include/pybind11/numpy.h
-- Installing: /usr/local/include/pybind11/complex.h
-- Installing: /usr/local/include/pybind11/embed.h
-- Installing: /usr/local/include/pybind11/chrono.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/pybind11Common.cmake
-- Installing: /usr/local/share/cmake/pybind11/pybind11Tools.cmake
-- Installing: /usr/local/share/cmake/pybind11/pybind11NewTools.cmake
-- Installing: /usr/local/share/cmake/pybind11/pybind11Targets.cmake
-- Installing: /usr/local/share/pkgconfig/pybind11.pc
vim ~/.bashrc
export PYTHONPATH=$PYTHONPATH:/home/pdd/CLionProjects/pybind11/pybind11/ # install location
-
source ~/.bashrc
-
参考: https://pybind11.readthedocs.io/en/stable/compiling.html#building-with-cmake
use
安装版本的方式报以下错误
/usr/local/include/pybind11/detail/common.h:266:10: fatal error: Python.h: 没有那个文件或目录
266 | #include <Python.h>
-
include_directories("/home/pdd/anaconda3/envs/yolocopy/include/python3.7m/")
-
target_link_libraries(untitled "/home/pdd/anaconda3/envs/yolocopy/lib/libpython3.7m.so")
Unknown CMake command "pybind11_add_module".
find_package(pybind11 CONFIG REQUIRED)
版本问题
- 对于版本问题,首先尝试了再安装导出so文件对应的python版本10下执行,然后更改到python3.7也正常运行了
python3.10
(base) pdd@pdd-Dell-G15-5511:~/CLionProjects/pybind11_examples-master/01_py-list_cpp-vector/cmake-build-debug$conda create -n py10 python=3.10
(base) pdd@pdd-Dell-G15-5511:~/CLionProjects/pybind11_examples-master/01_py-list_cpp-vector/cmake-build-debug$ conda activate py10
(py10) pdd@pdd-Dell-G15-5511:~/CLionProjects/pybind11_examples-master/01_py-list_cpp-vector/cmake-build-debug$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake example.cbp example.cpython-310-x86_64-linux-gnu.so Makefile pybind11
(py10) pdd@pdd-Dell-G15-5511:~/CLionProjects/pybind11_examples-master/01_py-list_cpp-vector/cmake-build-debug$ python
Python 3.10.9 (main, Jan 11 2023, 15:21:40) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import example
>>> A = [1.,2.,3.,4.]
>>> B = example.modify(A)
>>> print(B)
[2.0, 4.0, 6.0, 8.0]
>>> exit()
python3.7
- 按照这种方法更改(设置python3.7对应的目录)bashrc没起作用
# [How to set the default library path for python](https://stackoverflow.com/questions/42548750/how-to-set-the-default-library-path-for-python)
export PYTHON_LIBRARY=/ghome/mypath/anaconda2/lib/libpython2.7.so
export PYTHON_INCLUDE_DIR=/ghome/mypath/anaconda2/include:/ghome/mypath/anaconda2/include/python2.7
- 但是这个起作用了
(base) pdd@pdd-Dell-G15-5511:/usr/bin$ hisrory
446 ls pyth*
447 rm python3
448 sudo rm python3
449 ls
450 ls pytho
451 ls pytho*
452 sudo ln -s /home/pdd/anaconda3/envs/yolocopy/bin/python3.7 ./python3
453 hisrory
- 运行成功
(yolocopy) pdd@pdd-Dell-G15-5511:~/CLionProjects/pybind11_examples-master/01_py-list_cpp-vector/cmake-build-debug$ python
Python 3.7.12 | packaged by conda-forge | (default, Oct 26 2021, 06:08:21)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import example
>>> A = [1.,2.,3.,4.]
>>> B = example.modify(A)
>>> print(B)
[2.0, 4.0, 6.0, 8.0]
- 直接拷贝so到其他位置也能运行
CG
cmake_minimum_required(VERSION 2.8.12)
project(example)
#set(PYBIND11_PYTHON_VERSION "3.7")
#include_directories("/usr/include/python3.10/")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
#find_package(OpenCV)
#include_directories(${OpenCV})
include_directories("/home/pdd/anaconda3/envs/yolocopy/bin/")
#link_libraries(${OpenCV_LIBS})
#target_link_libraries(example ${OpenCV_LIBS})
add_subdirectory(pybind11)
pybind11_add_module(example example.cpp)
#add_executable(untitled example.cpp)
target_link_libraries(${PROJECT_NAME} INTERFACE
${OpenCV_LIBRARIES}
pybind11::pybind11
)