"Syntax error in input" when SWIGging Boost.Geometry?

"Syntax error in input" when SWIGging Boost.Geometry?

本文关键字:Boost Geometry SWIGging input Syntax error in when      更新时间:2023-10-16

错误信息:

Error: Syntax error in input(1)

我的Swig文件:

%module interfaces
%{
#include <vector>
#include <list>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/linestring.hpp>
typedef boost::geometry::model::d2::point_xy<double> Point;
typedef boost::geometry::model::polygon<Point, true, false> Polygon;
%}
%include "std_vector.i"
%template(MultiPolygon) std::vector<Polygon>;
%template(pgon) Polygon;

如果我注释掉最后一行,它会编译

// %template(pgon) Polygon;

我一直在重新阅读关于模板的swig部分,我完全不明白哪里出了问题。我做错了什么?我该如何改正?

即使Polygon是专门化的类型定义别名,您仍然需要将%template与您关心的实际模板一起使用,例如:

%template(pgon) polygon<Point, true, false>;

您还需要向SWIG展示所涉及的类型的足够的定义/声明,以便它弄清楚发生了什么并使用正确的类型。

因此,最小的完整接口文件是:

%module poly
%{
#include <vector>
#include <list>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/linestring.hpp>
%}
%inline %{
typedef boost::geometry::model::d2::point_xy<double> Point;
typedef boost::geometry::model::polygon<Point, true, false> Polygon;
%}
namespace boost {
namespace geometry {
namespace model {
template<typename P, bool CW, bool CL> struct polygon {};
namespace d2 {
template <typename T> struct point_xy {};
}
}
}
}
%include "std_vector.i"
%template(Point) boost::geometry::model::d2::point_xy<double>;
%template(pgon) boost::geometry::model::polygon<Point, true, false>;
%template(MultiPolygon) std::vector<Polygon>;

这是因为SWIG需要知道它包装的每个类型的定义以及%template指令。您还需要使您编写的类型对SWIG和c++编译器都可见,我在%inline中这样做是为了避免重复它们。