SWIG - a problem with namespaces

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 arises as a Python module, if I do not transfer it to the namespace, but as soon as I do this, 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 to build the package in 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 choose a namespace. Although SWIG handled namespaces automatically in simple cases like this. What am I doing wrong?

This is the error I get - it looks like SWIG refers to the character "ns" and "namespace", 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’
+3
1

test.i "using namespace ns" #include. swig , Foo ns.

+12

Source: https://habr.com/ru/post/1764393/


All Articles