3

For example, I have test.cpp:

#include<iostream>
using namespace std;

void hello(){
    cout<<"Hello World!"<<endl;
}

I write test.i:

%module test

%{
#include<iostream>
%}

void hello();

When I compile test_wrap.cxx, it tell me hello() is not declare, and I change test.i to:

%module test

%{
#include<iostream>
void hello();
%}

void hello();

It pass the compiling, I'm confused because I see some demos don't write function declaration in %{ %{, and why we need to write void hello(); twice?

gaussclb
  • 1,217
  • 3
  • 13
  • 26
  • Instead of declaring functions in the interface file manually, you are [encouraged](http://www.swig.org/Doc3.0/SWIGDocumentation.html#SWIG_nn46) to `%include` your header file. Doesn’t it work for you? – Melebius Dec 19 '17 at 08:17

1 Answers1

3

The code between %{ and %} is copied directly into the generated wrapper. The wrapper needs to know that void hello(); is external.

The code outside the %{ and %} markers generates wrapper code, so void hello(); generates wrapper code for the function.

To avoid the repetition you can use:

%inline %{
    void hello();
%}

This both adds the code to the wrapper and generates the wrappers.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251