【发布时间】:2011-04-11 09:33:56
【问题描述】:
我无法让以下简单示例与 SWIG 1.3.40 一起使用(我也尝试了 1.3.31)。只要我不将 Foo 结构包装在命名空间中,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”和“命名空间”符号。
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’
【问题讨论】:
-
你能把生成的文件test_wrap.c的相关部分贴出来吗?请注意,默认情况下 g++ 会查看文件扩展名以确定文件使用的语言,因此 test_wrap.c 将被编译为 C 代码,而不是 C++。
标签: c++ python macos namespaces swig