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

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

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

/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> >]’
 1195 |         if ((... || !std::get<Is>(argcasters).load(call.args[Is], call.args_convert[Is])))
      |                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

这是我的c++文件

#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>

namespace py = pybind11;

using MatrixX3d = Eigen::Matrix<double, Eigen::Dynamic, 3, Eigen::RowMajor>;

PYBIND11_MODULE(test, m) {
    m.def("func", [](const Eigen::Map<MatrixX3d>& x)
    {
        // ...
    });
}

和我的cmake文件

cmake_minimum_required(VERSION 3.12)
project(test)

set(CMAKE_CXX_STANDARD 17)

find_package(pybind11 CONFIG REQUIRED)
find_package(Eigen3 REQUIRED)

add_library(test MODULE test.cpp)
target_link_libraries(test PUBLIC pybind11::module Eigen3::Eigen)

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

*  Executing task: CMake: build 

build task started....
/usr/bin/cmake --build /home/me/dev/test/buildDebug --config Debug --target all -j 10 --
Consolidate compiler generated dependencies of target test
[ 50%] Building CXX object CMakeFiles/test.dir/test.cpp.o
In file included from /usr/include/pybind11/attr.h:13,
                 from /usr/include/pybind11/pybind11.h:13,
                 from /home/me/dev/test/test.cpp:1:
/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>]’:
/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> >&}]’
/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}]’
/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]’
/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 = {}]’
/home/me/dev/test/test.cpp:11:10:   required from here
/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> >]’
 1195 |         if ((... || !std::get<Is>(argcasters).load(call.args[Is], call.args_convert[Is])))
      |                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/me/dev/test/test.cpp:2:
/usr/include/pybind11/eigen.h:393:10: note: declared here
  393 |     bool load(handle, bool) = delete;
      |          ^~~~
In file included from /usr/include/pybind11/attr.h:13,
                 from /usr/include/pybind11/pybind11.h:13,
                 from /home/me/dev/test/test.cpp:1:
/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> >&]’:
/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>]’
/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]’
/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}]’
/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]’
/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 = {}]’
/home/me/dev/test/test.cpp:11:10:   required from here
/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> >]’
   42 |     return std::move(caster).operator
      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~                                                               
   43 |         typename make_caster<T>::template cast_op_type<typename std::add_rvalue_reference<T>::type>();
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
In file included from /home/me/dev/test/test.cpp:2:
/usr/include/pybind11/eigen.h:394:5: note: declared here
  394 |     operator MapType() = delete;
      |     ^~~~~~~~
gmake[2]: *** [CMakeFiles/test.dir/build.make:76: CMakeFiles/test.dir/test.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/test.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2
build finished with error(s).

 *  The terminal process terminated with exit code: 2. 
 *  Terminal will be reused by tasks, press any key to close it.
fcwjkofz

fcwjkofz1#

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

相关问题