c++ - SWIG - 命名空间问题

标签 c++ python macos namespaces swig

我无法让以下简单示例与 SWIG 1.3.40 一起使用(我也尝试过 1.3.31)。只要我不将 Foo 结构包装在命名空间中,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 上构建 bundle :

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.i 文件中,在 #include 之后添加“using namespace ns”行。否则,您的 swig 包装器代码将不知道在“ns”命名空间中查找 Foo。

关于c++ - SWIG - 命名空间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3696084/

相关文章:

c++ - 如何在不引入循环包含的情况下构建它?

c++ - 以二进制方式将utf16写入文件

c++ - 在 linux 中创建 "virtual"OpenGL 上下文

macos - .dylib 动态链接库可以打包成可执行文件吗?

python - Google App Engine 中的 key 生成

macos - 无法使用 nfs 挂载 vagrant 同步文件夹

c++ - 如何概括 spirit 解析器以任意顺序获取列表?

c++ - 在 Netbeans 中更改库路径

python - 是否可以在排列问题中不使用嵌套循环?

python - wxpython:制作窗口时如何使窗口全尺寸?