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?
#import and #include are preprocessor directives for bringing in the contents of a header to a file. #include is replaced by the contents of the header directly, while #import is only replaced by the contents of the header the first time that header is imported.
The difference between the two forms is in the location where the preprocessor searches for the file to be included. The preprocessor searches in an implementation-dependent manner, it searches directories pre-designated by the compiler. This method is usually used to include standard library header files.
The #include directive tells the C preprocessor to include the contents of the file specified in the input stream to the compiler and then continue with the rest of the original file.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program.
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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With