gaussclb
gaussclb

Reputation: 1247

How to write the SWIG interface file?

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?

Upvotes: 3

Views: 1084

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 177901

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.

Upvotes: 3

Related Questions