views:

40

answers:

1

I'm having trouble getting the following simple example to work with SWIG 1.3.40 (and I also tried 1.3.31). The Foo structure comes through as a Python module as long as I don't wrap it in a namespace, but as soon as I do I get a compilation error in the generated test_wrap.c.

test.h:

#ifndef __TEST_H__
#define __TEST_H__

#define USE_NS 1

#if USE_NS
namespace ns {
#endif

struct Foo {
  float a;
  float b;
  float func();
};

#if USE_NS
}
#endif

#endif

test.cpp

#include "test.h"

#if USE_NS
namespace ns {
#endif

float Foo::func()
{
  return a;
}

#if USE_NS
}
#endif

test.i

%module test
%{
#include "test.h"
%}

%include "test.h"

I run the following commands for building a bundle on OSX 10.6.3:

swig -python test.i
g++ -c -m64 -fPIC test.cpp
g++ -c -m64 -fPIC -I/usr/local/include -I/opt/local/include -I/opt/local/Library/Frameworks/Python.framework/Headers test_wrap.c
g++ -o _test.so -bundle -flat_namespace -undefined suppress test_wrap.o test.o -L/usr/local/lib -L/opt/local/lib -lpython2.6

This works, but only if I take out the namespace. I though SWIG handled namespaces automatically in simple cases like this. What am I doing wrong?

This is the error that I get - it looks like SWIG references a 'ns' and a 'namespace' symbol which are undefined.

test_wrap.c: In function ‘int Swig_var_ns_set(PyObject*)’:
test_wrap.c:2721: error: expected primary-expression before ‘=’ token
test_wrap.c:2721: error: expected primary-expression before ‘namespace’
test_wrap.c:2721: error: expected `)' before ‘namespace’
test_wrap.c:2721: error: expected `)' before ‘;’ token
test_wrap.c: In function ‘PyObject* Swig_var_ns_get()’:
test_wrap.c:2733: error: expected primary-expression before ‘void’
test_wrap.c:2733: error: expected `)' before ‘void’
A: 

In your test.i file, add a "using namespace ns" line after the #include. Without that, your swig wrapper code won't know to look for Foo in the "ns" namespace.

Gabriel Burca