“%include”和“%include”之间的区别和“#include”
在 SWIG 中,“%include”指令和标准 C“#include”有什么区别?
例如,在所有教程中,为什么它们通常看起来像这样:
%module my_module
%{
#include "MyHeader.h"
%}
%include "MyHeader.h"
这对我来说似乎是多余的。也许有了解的人可以解释一下。
是否有包含 C++ 代码的首选方法?
In SWIG, what is the difference between the "%include" directive, and the standard C "#include"?
For instance, in all the tutorials, why do they typically look something like this:
%module my_module
%{
#include "MyHeader.h"
%}
%include "MyHeader.h"
This seems redundant to me. Perhaps somebody with knowledge can clarify.
Is there a preferred method for including C++ code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
%{ ... %}
中的内容直接传递到输出;它本身不由 SWIG 解释。因此,#include 的作用是确保生成的 C/C++ 代码包含该标头。相比之下,
%include
是一个 SWIG 指令。它告诉 SWIG 在继续之前处理该头文件。这样,SWIG 将了解该头文件中声明的类型和函数(并为其生成包装器)。如果标头非常复杂,它可能会混淆 SWIG 或导致非常大的输出(因为 SWIG 尝试为其中的所有内容生成包装器)。在这种情况下,您最好手动声明仅需要 SWIG 处理的标头部分,并省略
%include
。但您可能仍然需要#include
以便编译生成的 C++。[更新]
至于“首选”,SWIG 更多的是关于什么是有效的而不是“首选”...如果您有一个非常干净的头文件,为单个类声明了一个很好的接口,您可以只
%include< /code> 它并让 SWIG 自动生成包装器。如果你的头文件非常多(例如
iostream
),你应该手动告诉 SWIG 要包装什么。但没有硬性规定。The stuff in
%{ ... %}
is passed through directly to the output; it is not itself interpreted by SWIG. So the#include
is there to make sure the generated C/C++ code includes that header.%include
, by contrast, is a SWIG directive. It tells SWIG to process that header file before proceeding. This way SWIG will learn about (and generate wrappers for) the types and functions declared in that header file.If the header is very complex, it might confuse SWIG or result in very large output (as SWIG tries to generate a wrapper for everything within). In such cases, you are better off manually declaring just the parts of the header that you need SWIG to process, and omit the
%include
. But you still may want the#include
in order for the generated C++ to compile.[update]
As for "preferred", SWIG is more about what works than what is "preferred"... If you have a very clean header file declaring a nice interface for a single class, you can just
%include
it and have SWIG automatically generate the wrappers. If your header file is very hairy (e.g.iostream
), you should manually tell SWIG what to wrap. But there is no hard and fast rule.%include 包含每个文件一次,这意味着您不需要包含防护。默认情况下,除非您使用 -includeall 选项运行 SWIG,否则 #include 会被忽略。
此外,预处理器会忽略 %{ 和 %} 之间的任何内容,并在不对输出进行任何修改的情况下进行复制。
更多内容,您可以阅读以下内容: http://www.swig.org/Doc1.3/预处理器.html。
%include includes each files once, which means you don't need include-guards. By default, the #include is ignored unless you run SWIG with the -includeall option.
Also, anything that between %{ and %} is ignored by the pre-processor and copied without any modification to the output.
Fore more you could read this: http://www.swig.org/Doc1.3/Preprocessor.html.