c++ pybind11和eigen给出“错误:使用已删除的函数'bool pybind11::detail::eigen_map_caster< MapType>::load”

wnavrhmk  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(121)

我试图用pybind11和eigen编译一个简单的c++程序,但是当我编译它时,我得到了(参见下面的完整错误信息)

  1. /usr/include/pybind11/cast.h:1195:51: error: use of deleted function bool pybind11::detail::eigen_map_caster<MapType>::load(pybind11::handle, bool) [with MapType = Eigen::Map<Eigen::Matrix<double, -1, 3, 1> >]’
  2. 1195 | if ((... || !std::get<Is>(argcasters).load(call.args[Is], call.args_convert[Is])))
  3. | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

这是我的c++文件

  1. #include <pybind11/pybind11.h>
  2. #include <pybind11/eigen.h>
  3. namespace py = pybind11;
  4. using MatrixX3d = Eigen::Matrix<double, Eigen::Dynamic, 3, Eigen::RowMajor>;
  5. PYBIND11_MODULE(test, m) {
  6. m.def("func", [](const Eigen::Map<MatrixX3d>& x)
  7. {
  8. // ...
  9. });
  10. }

和我的cmake文件

  1. cmake_minimum_required(VERSION 3.12)
  2. project(test)
  3. set(CMAKE_CXX_STANDARD 17)
  4. find_package(pybind11 CONFIG REQUIRED)
  5. find_package(Eigen3 REQUIRED)
  6. add_library(test MODULE test.cpp)
  7. target_link_libraries(test PUBLIC pybind11::module Eigen3::Eigen)

我使用的是https://pybind11.readthedocs.io/en/latest/advanced/cast/eigen.html文档。我做错了什么?
完整的错误消息为

  1. * Executing task: CMake: build
  2. build task started....
  3. /usr/bin/cmake --build /home/me/dev/test/buildDebug --config Debug --target all -j 10 --
  4. Consolidate compiler generated dependencies of target test
  5. [ 50%] Building CXX object CMakeFiles/test.dir/test.cpp.o
  6. In file included from /usr/include/pybind11/attr.h:13,
  7. from /usr/include/pybind11/pybind11.h:13,
  8. from /home/me/dev/test/test.cpp:1:
  9. /usr/include/pybind11/cast.h: In instantiation of bool pybind11::detail::argument_loader<Args>::load_impl_sequence(pybind11::detail::function_call&, std::index_sequence<Is ...>) [with long unsigned int ...Is = {0}; Args = {const Eigen::Map<Eigen::Matrix<double, -1, 3, 1, -1, 3>, 0, Eigen::Stride<0, 0> >&}; std::index_sequence<Is ...> = std::integer_sequence<long unsigned int, 0>]’:
  10. /usr/include/pybind11/cast.h:1173:34: required from bool pybind11::detail::argument_loader<Args>::load_args(pybind11::detail::function_call&) [with Args = {const Eigen::Map<Eigen::Matrix<double, -1, 3, 1, -1, 3>, 0, Eigen::Stride<0, 0> >&}]’
  11. /usr/include/pybind11/pybind11.h:214:42: required from void pybind11::cpp_function::initialize(Func&&, Return (*)(Args ...), const Extra& ...) [with Func = pybind11_init_test(pybind11::module_&)::<lambda(const Eigen::Map<Eigen::Matrix<double, -1, 3, 1> >&)>; Return = void; Args = {const Eigen::Map<Eigen::Matrix<double, -1, 3, 1, -1, 3>, 0, Eigen::Stride<0, 0> >&}; Extra = {pybind11::name, pybind11::scope, pybind11::sibling}]’
  12. /usr/include/pybind11/pybind11.h:100:19: required from pybind11::cpp_function::cpp_function(Func&&, const Extra& ...) [with Func = pybind11_init_test(pybind11::module_&)::<lambda(const Eigen::Map<Eigen::Matrix<double, -1, 3, 1> >&)>; Extra = {pybind11::name, pybind11::scope, pybind11::sibling}; <template-parameter-1-3> = void]’
  13. /usr/include/pybind11/pybind11.h:1048:22: required from pybind11::module_& pybind11::module_::def(const char*, Func&&, const Extra& ...) [with Func = pybind11_init_test(pybind11::module_&)::<lambda(const Eigen::Map<Eigen::Matrix<double, -1, 3, 1> >&)>; Extra = {}]’
  14. /home/me/dev/test/test.cpp:11:10: required from here
  15. /usr/include/pybind11/cast.h:1195:51: error: use of deleted function bool pybind11::detail::eigen_map_caster<MapType>::load(pybind11::handle, bool) [with MapType = Eigen::Map<Eigen::Matrix<double, -1, 3, 1> >]’
  16. 1195 | if ((... || !std::get<Is>(argcasters).load(call.args[Is], call.args_convert[Is])))
  17. | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  18. In file included from /home/me/dev/test/test.cpp:2:
  19. /usr/include/pybind11/eigen.h:393:10: note: declared here
  20. 393 | bool load(handle, bool) = delete;
  21. | ^~~~
  22. In file included from /usr/include/pybind11/attr.h:13,
  23. from /usr/include/pybind11/pybind11.h:13,
  24. from /home/me/dev/test/test.cpp:1:
  25. /usr/include/pybind11/cast.h: In instantiation of typename pybind11::detail::make_caster<T>::cast_op_type<typename std::add_rvalue_reference<_Tp>::type> pybind11::detail::cast_op(pybind11::detail::make_caster<T>&&) [with T = const Eigen::Map<Eigen::Matrix<double, -1, 3, 1> >&; typename pybind11::detail::make_caster<T>::cast_op_type<typename std::add_rvalue_reference<_Tp>::type> = Eigen::Map<Eigen::Matrix<double, -1, 3, 1> >; pybind11::detail::make_caster<T> = pybind11::detail::type_caster<Eigen::Map<Eigen::Matrix<double, -1, 3, 1> >, void>; typename std::add_rvalue_reference<_Tp>::type = const Eigen::Map<Eigen::Matrix<double, -1, 3, 1> >&]’:
  26. /usr/include/pybind11/cast.h:1207:51: required from Return pybind11::detail::argument_loader<Args>::call_impl(Func&&, std::index_sequence<Is ...>, Guard&&) && [with Return = void; Func = pybind11_init_test(pybind11::module_&)::<lambda(const Eigen::Map<Eigen::Matrix<double, -1, 3, 1> >&)>&; long unsigned int ...Is = {0}; Guard = pybind11::detail::void_type; Args = {const Eigen::Map<Eigen::Matrix<double, -1, 3, 1, -1, 3>, 0, Eigen::Stride<0, 0> >&}; std::index_sequence<Is ...> = std::integer_sequence<long unsigned int, 0>]’
  27. /usr/include/pybind11/cast.h:1184:65: required from std::enable_if_t<std::is_void<_Dummy>::value, pybind11::detail::void_type> pybind11::detail::argument_loader<Args>::call(Func&&) && [with Return = void; Guard = pybind11::detail::void_type; Func = pybind11_init_test(pybind11::module_&)::<lambda(const Eigen::Map<Eigen::Matrix<double, -1, 3, 1> >&)>&; Args = {const Eigen::Map<Eigen::Matrix<double, -1, 3, 1, -1, 3>, 0, Eigen::Stride<0, 0> >&}; std::enable_if_t<std::is_void<_Dummy>::value, pybind11::detail::void_type> = pybind11::detail::void_type]’
  28. /usr/include/pybind11/pybind11.h:233:71: required from void pybind11::cpp_function::initialize(Func&&, Return (*)(Args ...), const Extra& ...) [with Func = pybind11_init_test(pybind11::module_&)::<lambda(const Eigen::Map<Eigen::Matrix<double, -1, 3, 1> >&)>; Return = void; Args = {const Eigen::Map<Eigen::Matrix<double, -1, 3, 1, -1, 3>, 0, Eigen::Stride<0, 0> >&}; Extra = {pybind11::name, pybind11::scope, pybind11::sibling}]’
  29. /usr/include/pybind11/pybind11.h:100:19: required from pybind11::cpp_function::cpp_function(Func&&, const Extra& ...) [with Func = pybind11_init_test(pybind11::module_&)::<lambda(const Eigen::Map<Eigen::Matrix<double, -1, 3, 1> >&)>; Extra = {pybind11::name, pybind11::scope, pybind11::sibling}; <template-parameter-1-3> = void]’
  30. /usr/include/pybind11/pybind11.h:1048:22: required from pybind11::module_& pybind11::module_::def(const char*, Func&&, const Extra& ...) [with Func = pybind11_init_test(pybind11::module_&)::<lambda(const Eigen::Map<Eigen::Matrix<double, -1, 3, 1> >&)>; Extra = {}]’
  31. /home/me/dev/test/test.cpp:11:10: required from here
  32. /usr/include/pybind11/cast.h:43:100: error: use of deleted function pybind11::detail::eigen_map_caster<MapType>::operator MapType() [with MapType = Eigen::Map<Eigen::Matrix<double, -1, 3, 1> >]’
  33. 42 | return std::move(caster).operator
  34. | ~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. 43 | typename make_caster<T>::template cast_op_type<typename std::add_rvalue_reference<T>::type>();
  36. | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
  37. In file included from /home/me/dev/test/test.cpp:2:
  38. /usr/include/pybind11/eigen.h:394:5: note: declared here
  39. 394 | operator MapType() = delete;
  40. | ^~~~~~~~
  41. gmake[2]: *** [CMakeFiles/test.dir/build.make:76: CMakeFiles/test.dir/test.cpp.o] Error 1
  42. gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/test.dir/all] Error 2
  43. gmake: *** [Makefile:91: all] Error 2
  44. build finished with error(s).
  45. * The terminal process terminated with exit code: 2.
  46. * Terminal will be reused by tasks, press any key to close it.
fcwjkofz

fcwjkofz1#

我发现了错误:我使用的是Eigen::Map而不是Eigen::Ref

相关问题