gpt4 book ai didi

compiler-errors - SWIG 生成的输出缺少命名空间限定符

转载 作者:行者123 更新时间:2023-12-02 10:48:03 26 4
gpt4 key购买 nike

我有一个模板数组,我用 swig 包装它以与 Python 一起使用。我创建了模板的两个实例。第一个的包装器代码是正确的,但是由于生成的代码中缺少命名空间限定符(请参阅下面的 copy_from'),我在第二个时遇到了编译错误。

我不知道 SWIG 是否对两个命名空间 api 和 sim 感到困惑,或者我的声明中是否缺少其他限定符。注意下面的“copy_from()”只是一个例子;同样的错误发生在其他 api::array 包装函数中。我正在构建的 SWIG 接口(interface) (.i) 文件
包括并包装如下所示的所有类型。

更新:我查看了我的 .i 文件,以确保所有包装类型都以在 C++ 中编译时遇到的相同顺序包含在内,即:

%include "mat3d.h"
%include "XMVECTOR.h"
%include "api_array.h" // api::array template decl.
%include "api_array_types.h" // array<XMVECTOR> instantiation
%include "array_types.h" // api::array<mat3d> instantiation

更新 2:我添加了一个模板别名(参见更新 2:在代码中添加了别名):
模板使用数组 = api::array;
SWIG 现在添加了 sim 命名空间,例如sim::array 。现在没有编译错误,因为别名 sim::array 映射到 api::array - 但仍然不明白为什么。不喜欢它生成两个不同的命名空间引用同一个模板类......
namespace api { 
template < typename T > class array : public wrapped<array<typename array_internal_type<T> >, typename array_internal_type<T> >
{
...
int copy_from( const array<T>& src ); // member generating an error

};
}

class XMVECTOR {...} declared in global namespace

namespace api {
extern template class array<XMVECTOR>;
%template(xyzV) array<XMVECTOR>;
}

class mat3d { ... } // declared in global namespace

namespace sim {
template <typename T> using array = api::array<T>; // UPDATE 2: added template alias
extern template class api::array<mat3d>;
%template(matrixV) api::array<mat3d>;
}

// sample SWIG-generated code... this wrapper compiles and executes correctly
SWIGINTERN PyObject *_wrap_xyzV_copy_from( PyObject *SWIGUNUSEDPARM( self ), PyObject *args ) {
PyObject *resultobj = 0;
api::array< XMVECTOR > *arg1 = (api::array< XMVECTOR > *) 0;
api::array< XMVECTOR > *arg2 = 0;
...
}


// this wrapper fails to compile
SWIGINTERN PyObject *_wrap_matrixV_copy_from( PyObject *SWIGUNUSEDPARM( self ), PyObject *args ) {
PyObject *resultobj = 0;
api::array< mat3d > *arg1 = (api::array_copy_from mat3d > *) 0;
array< mat3d > *arg2 = 0; // error: no namespace on array; should be api::array. Applying update #2, this is "fixed" as sim::array< mat3d >
...
}

最佳答案

这似乎可以通过将 sim 命名空间 block 之外的 %template 指令移动到全局来解决:

        // old: 
namespace sim { ...
%template(matrixV) api::array<mat3d>;
}

// new:
namespace sim { ... }
%template(matrixV) api::array<mat3d>;

...现在生成正确的命名空间 (api::)
 SWIGINTERN PyObject *_wrap_matrixV_copy_from(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
api::array< mat3d > *arg1 = (api::array< mat3d > *) 0 ;
api::array< mat3d > *arg2 = 0 ; // correct api:: namespace added

不知道为什么 SWIG 没有在这里正确识别成员函数参数:
  template < typename T > class array 
{
copy_from( const array<T>& src ); // arg. 'src' is same type as template class (api::array)
}

如果 %template 恰好放置在命名空间中,我可能会误解 SWIG 如何解释它。无论如何,我会假设这个问题现在已经解决了。

关于compiler-errors - SWIG 生成的输出缺少命名空间限定符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41940545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com