Swig:包装全局静态常量时出现语法错误
Swig: syntax error when wrapping global static constants
当我尝试包装以下代码时:
enum VehicleSide {
LEFT = 0, ///< left side of vehicle is always 0
RIGHT = 1 ///< right side of vehicle is always 1
};
/// Class to encode the ID of a vehicle wheel.
/// By convention, wheels are counted front to rear and left to right. In other
/// words, for a vehicle with 2 axles, the order is: front-left, front-right,
/// rear-left, rear-right.
class WheelID {
public:
WheelID(int id) : m_id(id), m_axle(id / 2), m_side(VehicleSide(id % 2)) {}
WheelID(int axle, VehicleSide side) : m_id(2 * axle + side), m_axle(axle), m_side(side) {}
/// Return the wheel ID.
int id() const { return m_id; }
/// Return the axle index for this wheel ID.
/// Axles are counted from the front of the vehicle.
int axle() const { return m_axle; }
/// Return the side for this wheel ID.
/// By convention, left is 0 and right is 1.
VehicleSide side() const { return m_side; }
private:
int m_id; ///< wheel ID
int m_axle; ///< axle index (counted from the front)
VehicleSide m_side; ///< vehicle side (LEFT: 0, RIGHT: 1)
};
/// Global constant wheel IDs for the common topology of a 2-axle vehicle.
static const WheelID FRONT_LEFT(0, LEFT);
static const WheelID FRONT_RIGHT(0, RIGHT);
static const WheelID REAR_LEFT(1, LEFT);
static const WheelID REAR_RIGHT(1, RIGHT);
我在 static const WheelID FRONT_LEFT(0, LEFT); 得到 "syntax error in input"。
在接口文件上,我只是在相应的 header 上使用 %include。
我不知道是什么导致了错误,所以任何帮助表示赞赏,但我不想编辑 header。
谢谢
编辑:
删除 static 关键字无济于事
显示您的 SWIG .i 文件会有所帮助,但原因很可能是您将 class 的实例放入了 header 文件。我看到您不想编辑 header,但是 .h 文件应该 extern
class 实例并且定义应该在 .cpp 文件中;否则,如果该文件包含在两个不同的 .cpp 文件中,您将拥有多个单独的全局变量实例。另外,SWIG 不理解它,所以你真的别无选择...
工作示例:
test.h
enum VehicleSide {
LEFT = 0, ///< left side of vehicle is always 0
RIGHT = 1 ///< right side of vehicle is always 1
};
/// Class to encode the ID of a vehicle wheel.
/// By convention, wheels are counted front to rear and left to right. In other
/// words, for a vehicle with 2 axles, the order is: front-left, front-right,
/// rear-left, rear-right.
class WheelID {
public:
WheelID(int id) : m_id(id), m_axle(id / 2), m_side(VehicleSide(id % 2)) {}
WheelID(int axle, VehicleSide side) : m_id(2 * axle + side), m_axle(axle), m_side(side) {}
/// Return the wheel ID.
int id() const { return m_id; }
/// Return the axle index for this wheel ID.
/// Axles are counted from the front of the vehicle.
int axle() const { return m_axle; }
/// Return the side for this wheel ID.
/// By convention, left is 0 and right is 1.
VehicleSide side() const { return m_side; }
private:
int m_id; ///< wheel ID
int m_axle; ///< axle index (counted from the front)
VehicleSide m_side; ///< vehicle side (LEFT: 0, RIGHT: 1)
};
// Instances of these global variables must be outside the header.
// What if the header was included in two files?
extern const WheelID FRONT_LEFT;
extern const WheelID FRONT_RIGHT;
extern const WheelID REAR_LEFT;
extern const WheelID REAR_RIGHT;
test.i
%module test
%{ // Section included verbatim in the generated wrapper source.
#include "test.h"
// One-time instances of the class. In this case, directly
// added to the wrapper, but it could be in a separate .cpp file
// or .lib and linked to the final extension.
const WheelID FRONT_LEFT(0, LEFT);
const WheelID FRONT_RIGHT(0, RIGHT);
const WheelID REAR_LEFT(1, LEFT);
const WheelID REAR_RIGHT(1, RIGHT);
%}
// Generate wrappers for test.h contents
%include "test.h"
演示:
>>> import test
>>> test.REAR_RIGHT.id()
3
当我尝试包装以下代码时:
enum VehicleSide {
LEFT = 0, ///< left side of vehicle is always 0
RIGHT = 1 ///< right side of vehicle is always 1
};
/// Class to encode the ID of a vehicle wheel.
/// By convention, wheels are counted front to rear and left to right. In other
/// words, for a vehicle with 2 axles, the order is: front-left, front-right,
/// rear-left, rear-right.
class WheelID {
public:
WheelID(int id) : m_id(id), m_axle(id / 2), m_side(VehicleSide(id % 2)) {}
WheelID(int axle, VehicleSide side) : m_id(2 * axle + side), m_axle(axle), m_side(side) {}
/// Return the wheel ID.
int id() const { return m_id; }
/// Return the axle index for this wheel ID.
/// Axles are counted from the front of the vehicle.
int axle() const { return m_axle; }
/// Return the side for this wheel ID.
/// By convention, left is 0 and right is 1.
VehicleSide side() const { return m_side; }
private:
int m_id; ///< wheel ID
int m_axle; ///< axle index (counted from the front)
VehicleSide m_side; ///< vehicle side (LEFT: 0, RIGHT: 1)
};
/// Global constant wheel IDs for the common topology of a 2-axle vehicle.
static const WheelID FRONT_LEFT(0, LEFT);
static const WheelID FRONT_RIGHT(0, RIGHT);
static const WheelID REAR_LEFT(1, LEFT);
static const WheelID REAR_RIGHT(1, RIGHT);
我在 static const WheelID FRONT_LEFT(0, LEFT); 得到 "syntax error in input"。 在接口文件上,我只是在相应的 header 上使用 %include。 我不知道是什么导致了错误,所以任何帮助表示赞赏,但我不想编辑 header。 谢谢
编辑: 删除 static 关键字无济于事
显示您的 SWIG .i 文件会有所帮助,但原因很可能是您将 class 的实例放入了 header 文件。我看到您不想编辑 header,但是 .h 文件应该 extern
class 实例并且定义应该在 .cpp 文件中;否则,如果该文件包含在两个不同的 .cpp 文件中,您将拥有多个单独的全局变量实例。另外,SWIG 不理解它,所以你真的别无选择...
工作示例:
test.h
enum VehicleSide {
LEFT = 0, ///< left side of vehicle is always 0
RIGHT = 1 ///< right side of vehicle is always 1
};
/// Class to encode the ID of a vehicle wheel.
/// By convention, wheels are counted front to rear and left to right. In other
/// words, for a vehicle with 2 axles, the order is: front-left, front-right,
/// rear-left, rear-right.
class WheelID {
public:
WheelID(int id) : m_id(id), m_axle(id / 2), m_side(VehicleSide(id % 2)) {}
WheelID(int axle, VehicleSide side) : m_id(2 * axle + side), m_axle(axle), m_side(side) {}
/// Return the wheel ID.
int id() const { return m_id; }
/// Return the axle index for this wheel ID.
/// Axles are counted from the front of the vehicle.
int axle() const { return m_axle; }
/// Return the side for this wheel ID.
/// By convention, left is 0 and right is 1.
VehicleSide side() const { return m_side; }
private:
int m_id; ///< wheel ID
int m_axle; ///< axle index (counted from the front)
VehicleSide m_side; ///< vehicle side (LEFT: 0, RIGHT: 1)
};
// Instances of these global variables must be outside the header.
// What if the header was included in two files?
extern const WheelID FRONT_LEFT;
extern const WheelID FRONT_RIGHT;
extern const WheelID REAR_LEFT;
extern const WheelID REAR_RIGHT;
test.i
%module test
%{ // Section included verbatim in the generated wrapper source.
#include "test.h"
// One-time instances of the class. In this case, directly
// added to the wrapper, but it could be in a separate .cpp file
// or .lib and linked to the final extension.
const WheelID FRONT_LEFT(0, LEFT);
const WheelID FRONT_RIGHT(0, RIGHT);
const WheelID REAR_LEFT(1, LEFT);
const WheelID REAR_RIGHT(1, RIGHT);
%}
// Generate wrappers for test.h contents
%include "test.h"
演示:
>>> import test
>>> test.REAR_RIGHT.id()
3