我正在尝试使用 SWIG 将一个类包装在 python 的大型 C++ 代码库中,但在编译生成的 C++ 包装器时遇到一些问题。
我创建了一个基本接口(interface)文件,PCSearchResult.i:
%module PCSearchResult
%{
#include "PCSearchResult.h"
%}
%include "PCSearchResult.h"
我生成了 PCSearchResult_wrap.cxx:
swig -c++ -python PCSearchResult.i
并尝试使用以下方式进行编译:
g++ -c -fpic -I. -I$(OTHERINCS) -I/usr/include/python2.7 PCSearchResult_wrap.cxx
这会导致大量错误。以下是前 15 行:
PCSearchResult_wrap.cxx: In function 'PyObject* _wrap_new_PCSearchResult__SWIG_1(PyObject*, PyObject*)':
PCSearchResult_wrap.cxx:3226:3: error: 'Point3d' was not declared in this scope
PCSearchResult_wrap.cxx:3226:3: note: suggested alternative:
In file included from PointCloud.h:28:0,
from PCSearchResult.h:29,
from PCSearchResult_wrap.cxx:3044:
Point3d.h:43:7: note: 'Isis::Point3d'
PCSearchResult_wrap.cxx:3226:12: error: 'arg1' was not declared in this scope
PCSearchResult_wrap.cxx:3230:19: error: 'PointCloud' was not declared in this scope
PCSearchResult_wrap.cxx:3230:19: note: suggested alternative:
In file included from PCSearchResult.h:29:0,
from PCSearchResult_wrap.cxx:3044:
PointCloud.h:80:7: note: 'Isis::PointCloud'
PCSearchResult_wrap.cxx:3230:30: error: template argument 1 is invalid
PCSearchResult_wrap.cxx:3230:38: error: invalid type in declaration before '=' token
所有“未在此范围内声明”的错误让我认为我一定未能在正确的位置包含某些内容。我知道我拥有 PCSearchResult.h 中 #included 的所有正确头文件,因为在不使用 SWIG 时,所有内容都可以编译并运行良好。
是否还有其他地方需要提供有关我在尝试包装的头文件中使用的类(在其他头文件中定义)的 SWIG 信息?我已阅读SWIG and C++ chapter SWIG 文档,但我仍然对此感到困惑。
我在 Fedora 18 上使用 SWIG 版本 3.0.2 和 g++ 版本 4.7.2。
最佳答案
看起来 Point3d 在命名空间中。在您自己的源文件中,您可能使用完整的 decl,或者源文件中有一个 using 命名空间 decl。因此,请尝试在 .i 文件中添加 using namespace Isis;
:
%module PCSearchResult
%{
#include "PCSearchResult.h"
using namespace Isis;
%}
%include "PCSearchResult.h"
这会将其插入到wrap.cxx 文件中。
关于python - SWIG 生成的 C++ 包装器会导致许多编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24791548/