swig中避免'对[父]类......一无所知'错误 [英] Avoid 'nothing known about [parent] class...' error in swig

查看:240
本文介绍了swig中避免'对[父]类......一无所知'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在头文件中有两个A类啊

Let's say I have two classes A in header file A.h

// A.h
class A {
public:
  void foo();
};

和头文件B中的B

// B.h
class B : public A {
public:
  void bar()
};

我想为B类生成一个Swig包装器。接口文件看起来像这样。

I want to generate a Swig wrapper for class B. The interface file looks like this.

B.i
%{
#include "B.h"
%}

%include B.h

当运行swig时,它会退出并显示错误消息'a nothing about A',这很清楚,因为B继承自A,因此swig必须知道A才能生成接口。让我们进一步假设在A.h中有一些东西swig解析器无法解析它会在看到那些东西时产生错误。我突然决定,我实际上不仅需要在界面中使用bar而不是foo。有没有办法告诉swig,它实际上并没有看啊,因为我真的不需要B继承自A的东西?

When running swig, it quits with an error message 'nothing known about A', which is clear, since B inherits from A and thus swig must know about A to generate the interface. Lets further assume there is some stuff in A.h the swig parser can not parse and it generates an error, when it sees that stuff. I suddenly decide, that I actually don't only need bar in the interface and not foo. Is there A way to tell swig, that it doesn't actually look at A.h, since I don't really need the stuff B inherits from A?

推荐答案

您可以对基类使用%import。这让SWIG知道了这个类,但是不会生成包装器。来自 SWIG 2.0文档

You could use %import for the base class. This lets SWIG know about the class, but no wrappers will be generated. From the SWIG 2.0 documentation:


如果未定义任何基类,SWIG仍会生成正确的
类型关系。例如,接受Foo *的函数将接受
从Foo派生的任何对象,无论SWIG实际上
是否包装了Foo类。 如果你真的不想为基类生成包装器
,但是你想让警告静音,你可能会考虑使用%import指令来包含定义$ b的文件$ b Foo。 %import只收集类型信息,但不会生成
包装器。
或者,您可以在SWIG界面中将Foo定义为空类
或使用警告抑制。

If any base class is undefined, SWIG still generates correct type relationships. For instance, a function accepting a Foo * will accept any object derived from Foo regardless of whether or not SWIG actually wrapped the Foo class. If you really don't want to generate wrappers for the base class, but you want to silence the warning, you might consider using the %import directive to include the file that defines Foo. %import simply gathers type information, but doesn't generate wrappers. Alternatively, you could just define Foo as an empty class in the SWIG interface or use warning suppression.

这篇关于swig中避免'对[父]类......一无所知'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆