避免“对[父]类一无所知......”喝水时出错
假设我在头文件 Ah 中有两个类 A
// A.h
class A {
public:
void foo();
};
,在头文件 Bh 中有两个类 B,
// B.h
class B : public A {
public:
void bar()
};
我想为类 B 生成一个 Swig 包装器。接口文件如下所示。
B.i
%{
#include "B.h"
%}
%include B.h
运行 swig 时,它会退出并显示错误消息“对 A 一无所知”,这很清楚,因为 B 继承自 A,因此 swig 必须了解 A 才能生成接口。让我们进一步假设 Ah 中有一些东西,swig 解析器无法解析,并且当它看到这些东西时会生成错误。我突然决定,我实际上不仅需要界面中的 bar,而且不需要 foo。有没有一种方法可以告诉 swig,它实际上并不看 Ah,因为我真的不需要 B 从 A 继承的东西?
Let's say I have two classes A in header file A.h
// A.h
class A {
public:
void foo();
};
and B in header file B.h
// B.h
class B : public A {
public:
void bar()
};
I want to generate a Swig wrapper for class B. The interface file looks like this.
B.i
%{
#include "B.h"
%}
%include B.h
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 %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:
我拼凑了一个例子,只得到一个警告,表明对 A 一无所知。该扩展仍然可以正常构建,并且可以在不知道 A 的 bar() 的情况下调用 B 的 foo() 。下面是我为 Windows 生成 Python 扩展的示例:
构建输出
示例使用
文件
a.h
b.h
a.cpp
b.cpp
b.i
makefile
I threw together an example, and only get a warning that nothing is known about A. The extension still builds fine and can call B's foo() without knowing about A's bar(). Here's my example generating a Python extension for Windows:
Build output
Example use
Files
a.h
b.h
a.cpp
b.cpp
b.i
makefile