Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "%include" and "#include"

Tags:

c++

c

swig

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?

like image 981
J T Avatar asked Jun 08 '11 21:06

J T


People also ask

What is the difference between #import and #include in C ++?

#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.

What is the difference between include filename and #include headers filename?

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.

What is the meaning of #include in C language?

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.

Why we use include in program?

#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.


2 Answers

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.

like image 95
Nemo Avatar answered Sep 16 '22 18:09

Nemo


%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.

like image 36
Marius Bancila Avatar answered Sep 16 '22 18:09

Marius Bancila