SWIG - 命名空间问题

2024-03-29 09:20:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用SWIG 1.3.40(我也尝试了1.3.31)时遇到了以下简单示例的问题。Foo结构作为一个Python模块来传递,只要我不将它包装在一个名称空间中,但是一旦我在生成的test_wrap.c中得到编译错误。

试验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

测试.cpp

#include "test.h"

#if USE_NS
namespace ns {
#endif

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

#if USE_NS
}
#endif

测试.i

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

%include "test.h"

我运行以下命令在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

这是可行的,但前提是去掉名称空间。我认为SWIG在这种简单的情况下会自动处理名称空间。我做错什么了?

这是我得到的错误-看起来SWIG引用了未定义的“ns”和“namespace”符号。

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’

Tags: testifincludeuselocalerrorfloatnamespace