我无法使用以下简单示例来使用SWIG 1.3.40(我也尝试过1.3.31)。只要我不将它包装在命名空间中,Foo结构就会作为Python模块出现,但是一旦我这样做,我就会在生成的 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"
我在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’
答案 0 :(得分:14)
在test.i文件中,在#include之后添加“using namespace ns”行。没有它,你的swig包装器代码将不知道在“ns”命名空间中查找Foo。