【发布时间】:2021-07-13 20:18:29
【问题描述】:
我一直在尝试使用 swig 来包装一个 c++ 代码,其中函数采用矢量引用(以启用修改):
//cpp_code.cpp
void worker(vector<int> &v){
// do something
}
// cpp_code.i
/* File: cpp_code.i */
%module cpp_code
%include "std_vector.i"
%include "typemaps.i"
%{
#include "cpp_code.cpp"
%}
%template(MyVector) std::vector<int>;
//%template(MyVectorPtr) std::vector<int &>;
%include "cpp_code.cpp"
void worker(vector<int> &v);
如果我取消注释部分//%template(MyVectorPtr) std::vector<int &>;,我会收到很长的回溯:
In file included from C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32/bit+allocator.h:33,
from C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/allocator.h:46,
from C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/string:41,
from C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/stdexcept:39,
from dft_wrap.cxx:2743:
C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x4-w64-mingw32/8.1.0/include/c++/ext/new_allocator.h: In instantiation of 'cl__gnu_cxx::new_allocator<int&>':
C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x4-w64-mingw32/8.1.0/include/c++/bits/allocator.h:108:11: required from 'clstd::allocator<int&>'
C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x4-w64-mingw32/8.1.0/include/c++/bits/stl_vector.h:84:21: required from 'st std::_Vector_base<int&, std::allocator<int&> >'
C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x4-w64-mingw32/8.1.0/include/c++/bits/stl_vector.h:339:11: required from 'c std::vector<int&, std::allocator<int&> >'
dft_wrap.cxx:4748:45: required from here
C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x4-w64-mingw32/8.1.0/include/c++/ext/new_allocator.h:63:26: error: forming por to reference type 'int&'
typedef _Tp* pointer;
^~~~~~~
C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x4-w64-mingw32/8.1.0/include/c++/ext/new_allocator.h:64:26: error: forming por to reference type 'int&'
typedef const _Tp* const_pointer;
^~~~~~~~~~~~~
In file included from C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/string:41,
from C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/stdexcept:39,
from dft_wrap.cxx:2743:
C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x4-w64-mingw32/8.1.0/include/c++/bits/allocator.h: In instantiation of 'class::allocator<int&>':
C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x4-w64-mingw32/8.1.0/include/c++/bits/stl_vector.h:84:21: required from 'st std::_Vector_base<int&, std::allocator<int&> >'
C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x4-w64-mingw32/8.1.0/include/c++/bits/stl_vector.h:339:11: required from 'c std::vector<int&, std::allocator<int&> >'
dft_wrap.cxx:4748:45: required from here
C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x4-w64-mingw32/8.1.0/include/c++/bits/allocator.h:113:26: error: forming pointo reference type 'int&'
typedef _Tp* pointer;
^~~~~~~
C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x4-w64-mingw32/8.1.0/include/c++/bits/allocator.h:114:26: error: forming pointo reference type 'int&'
typedef const _Tp* const_pointer;
^~~~~~~~~~~~~
In file included from C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ext/alloc_traits.h:36,
from C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/basic_string.h:40
from C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/string:52,
from C:/ProgramData/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/stdexcept:39,
from dft_wrap.cxx:2743:
打包文件的创建没有问题,它也已编译,现在尝试在 python 中使用它;
PS C:\Users\me\graphy\graphy> python Python 3.7.6(默认,2020 年 1 月 8 日,20:23:39)[MSC v.1916 64 位 (AMD64)] :: Anaconda, Inc. on win32
Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https://conda.io/activation
Type "help", "copyright", "credits" or "license" for more information.
>>> from cpp_code import *
>>> worker([5])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\me\graphy\graphy\cpp_code.py", line 233, in cpp_code
return _cpp_code.worker(v)
TypeError: in method 'cpp_code', argument 1 of type 'vector< int > &'
>>>
如何“转换”对 python 列表的引用,我已经检查了文档和大量 stackOverflow 但无济于事,欢迎任何帮助。
谢谢。
【问题讨论】: