Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks
I am fairly new to Perl. For gluing my C++ components I decided to use SWIG. After reading the document and examples of using SWIG, I still have following doubts.
1) What exactly have to be included in %{ %} block? In some examples header files and some function declarations were made.But function was again declared outside the block as the one shown like function foo()
%module mymodule %{ #include file.h int foo(); %} int foo(); int bar();

Why is this double declaration of function foo()?
2) Some functions are only declared outside the block like the function bar(). What is the difference in declaring inside and outside the bloack?
3) What is difference between %include and %import.
Kindly enlighten me
Thanking you all in advance
Regards,
Ashu

Edit: g0n - added code tags

Replies are listed 'Best First'.
Re: Confusion regarding SWIG
by salva (Canon) on Dec 10, 2005 at 11:21 UTC
    inside the %{ ... %} blocks you have to include declarations for all the C++ funcions, classes and methods you want to wrap. That code is copied on the generated C++ source file as is.

    If you are proficient programing in C/C++, I advice you to take a look at the C++ code generated for simple examples, you will promptly understand what is going on.

Re: Confusion regarding SWIG
by eric256 (Parson) on Dec 09, 2005 at 16:54 UTC

    You might include what SWIG is or a link to it. The only thing close i could come up with was SWAG which i'm sure doesn't appply. (SWAG = "Scientific Wild Ass Guess" and is probably the only thing i remember my 6th grade teacher saying. If more teachers swore in class maybe i woul d have remembered more of what they were saying ;) )


    ___________
    Eric Hodges $_='y==QAe=e?y==QG@>@?iy==QVq?f?=a@iG?=QQ=Q?9'; s/(.)/ord($1)-50/eigs;tr/6123457/- \/|\\\_\n/;print;
      http://www.swig.org/
      http://en.wikipedia.org/wiki/SWIG
      "SWIG (Simplified Wrapper and Interface Generator) is a free computer software tool used to connect programs written in C/C++ with scripting languages such as Tcl, Perl, Python, Ruby, Guile, PHP and other languages like Java, C#, and Ocaml. The aim is to achieve this connection with minimal effort: a small number of directives are added to the program's header files. Running the SWIG tool creates source code which provides the glue between C/C++ and the target language. Depending on the language, this glue comes in three forms: an executable that embeds the interpreter for the scripting language, a shared library that links into an existing interpreter as some form of extension module, or a shared library that can be linked to other programs compiled in the target language (for example, using JNI)."