请说明SWIG发出的这些警告是什么以及如何避免?
Warning 503: Can't wrap 'operator ()' unless renamed to a valid identifier.
Warning 503: Can't wrap 'operator =' unless renamed to a valid identifier.
Warning 503: Can't wrap 'operator *' unless renamed to a valid identifier.
警告是在Android NDK下编译SWIG生成的C ++代码时生成的。
Java与C ++没有相同的含义,[Java没有operator()
或operator=
,因此SWIG无法直接包装它。因为它们可能很重要,所以会向您显示警告,说明它们没有被包装。 (有时operator=
可能会特别糟糕)。
此代码在运行swig -Wall -c++ -java
时显示出这样的警告:
%module Sample
struct test {
bool operator()();
};
但是您可以通过以下类似的方式使警告消失,并告诉SWIG将操作符直接作为常规成员函数公开:
%module Sample
%rename(something_else) operator();
struct test {
bool operator()();
};
这将在生成的包装器中添加一个名为something_else
的函数来代替operator()
。
或者您可以向SWIG断言,使用以下命令忽略它们就可以了:
%ignore operator()
((您也可以通过使用类名来限定运算符来将这些指令中的任一个应用到较不广泛的位置。
如果要在目标语言中使用重载运算符,则需要以特殊方式处理SWIG。参见here。
如果在实例化函数的%rename
指令之前出现%template
指令,则会遇到此警告:
%module some_module
%rename("%(undercase)s", %$isfunction) "";
// equivalently %rename("%(utitle)s", %$isfunction) "";
%inline %{
template<class T>
void MyFunction(){}
%}
// ...
%template(MyIntFunction) MyFunction<int>;
警告503:除非重命名为有效标识符,否则不能包装'my_function
'
而且您无法尝试在%template
中预期重命名:
%template(MyIntFunction) my_function<int>;
因为那样您会得到
错误:未定义模板'myfunction'。
如果要应用全局重命名规则,这将非常令人沮丧,并且您实际上仅需要“忽略重命名”就可以了。与类型映射不同,重命名指令始终存在。能够暂时关闭它们会很高兴。
我想出的唯一解决方法是回到%rename
并更新它,使其显式地仅匹配(或不匹配)您声明为内联的模板函数。像这样:
// don't rename functions starting with "MyFunction"
%rename("%(undercase)s", %$isfunction, notregexmatch$name="^MyFunction") "";
这并不完美,但这是一种解决方法。
(全部在SWIG 4.0中完成)