SWIG生成的C++包装器导致多重编译错误

2 投票
1 回答
2760 浏览
提问于 2025-04-18 13:36

我正在尝试使用SWIG将一个大型C++代码库中的一个类封装为Python,但在编译生成的C++封装时遇到了一些问题。

我创建了一个基本的接口文件,叫做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中已经包含了所有正确的头文件,因为在不使用SWIG时,一切都能正常编译和运行。

我是否需要在其他地方告诉SWIG我在尝试封装的头文件中使用的类信息(这些类是在其他头文件中定义的)?我已经阅读了SWIG和C++章节的SWIG文档,但对此仍然感到困惑。

我使用的是SWIG版本3.0.2和g++版本4.7.2,操作系统是Fedora 18。

1 个回答

4

看起来 Point3d 是在一个命名空间里。在你自己的源文件中,你可能使用了完整的声明,或者在源文件里有一个使用命名空间的声明。所以试着在 .i 文件中添加一行 using namespace Isis;

%module PCSearchResult
%{
#include "PCSearchResult.h"
using namespace Isis;
%}
%include "PCSearchResult.h"

这样做会把它插入到 wrap.cxx 文件里。

撰写回答