Swig命名空间错误?不是一个有效的基类
swig namespace error? not a valid base class
我使用SWIG来包装一个c++库。我得到一个错误,我认为是与我使用的名称空间,但我不确定。不幸的是,SWIG的文档似乎都集中在内联文档上,我不知道它从我的头文件中提取了什么。
这是我的。i文件:
%module cStopPow
%{
#include "../src/StopPow.h"
#include "../src/StopPow_SRIM.h"
#include "../src/StopPow_LP.h"
#include "../src/StopPow_BetheBloch.h"
%}
%include "cpointer.i"
%pointer_functions(int, intp);
%pointer_functions(float, floatp);
%include "std_vector.i"
#include <vector>
// Instantiate templates
namespace std {
%template(IntVector) vector<int>;
%template(FloatVector) vector<float>;
}
%include "std_string.i"
#include <string>
%include "../src/StopPow.h"
%include "../src/StopPow_SRIM.h"
%include "../src/StopPow_LP.h"
%include "../src/StopPow_BetheBloch.h"
这里是一个裁剪的示例头(它们的定义都非常相似):
#include ...
namespace StopPow
{
class StopPow_BetheBloch : StopPow
{ ...
三个类扩展了StopPow。该库在c++下编译得很好,但SWIG给了我以下错误:
swig -java -c++ StopPow.i
../src/StopPow_BetheBloch.h:26: Warning 319: No access specifier given for base class 'StopPow' (ignored).
../src/StopPow_SRIM.h:27: Error: 'StopPow' is not a valid base class.
../src/StopPow.h:28: Error: See definition of 'StopPow'.
../src/StopPow_SRIM.h:27: Warning 401: Nothing known about base class 'StopPow'. Ignored.
../src/StopPow_LP.h:27: Error: 'StopPow' is not a valid base class.
../src/StopPow.h:28: Error: See definition of 'StopPow'.
../src/StopPow_LP.h:27: Warning 401: Nothing known about base class 'StopPow'. Ignored.
../src/StopPow_BetheBloch.h:26: Error: 'StopPow' is not a valid base class.
../src/StopPow.h:28: Error: See definition of 'StopPow'.
make: *** [StopPow] Error 6
任何想法?
好的,所以经过多次故障排除后,我弄清楚了。当我将代码放入非std名称空间后,这些问题就出现了,当我的所有代码都在std名称空间中时,上面的。I文件可以正常工作。
有两个问题。首先,基类必须在. I文件的显式中定义,这在文档中有提到,但我错过了。而且,SWIG显然不支持定义了构造函数的基类。向StopPow类添加方法StopPow()会导致从它继承的三个类出现"错误:'StopPow'不是一个有效的基类"
这里是工作的停止点。
// StopPow.i - SWIG interface
%module cStopPow
%{
#include "../src/StopPow.h"
#include "../src/StopPow_SRIM.h"
#include "../src/StopPow_LP.h"
#include "../src/StopPow_BetheBloch.h"
%}
%include "cpointer.i"
%pointer_functions(int, intp);
%pointer_functions(float, floatp);
%include "std_vector.i"
#include <vector>
// Instantiate templates
namespace std {
%template(IntVector) vector<int>;
%template(FloatVector) vector<float>;
}
%include "std_string.i"
#include <string>
//%nspace StopPow::StopPow;
//%nspace StopPow::StopPow_LP;
// Need to define the base class:
namespace StopPow
{
class StopPow {
public:
//StopPow();
virtual float dEdx_MeV_um(float E) = 0;
virtual float dEdx_MeV_mgcm2(float E) = 0;
virtual float get_Emin() = 0;
virtual float get_Emax() = 0;
float dEdx(float E);
float Eout(float E, float x);
float Ein(float E, float x);
float Thickness(float E1, float E2);
float get_dx();
void set_dx(float new_dx);
int get_mode();
void set_mode(int new_mode);
static const float DEFAULT_DX;
static const float DEFAULT_DRHOR;
static const int MODE_LENGTH;
static const int MODE_RHOR;
};
};
%include "../src/StopPow_SRIM.h"
%include "../src/StopPow_LP.h"
%include "../src/StopPow_BetheBloch.h"
SWIG用构造函数/析构函数处理基类很好。问题是名称空间与类名称相同。示例(用于Python):
demo.i
%module demo
%begin %{
#pragma warning(disable:4127 4100 4211 4701 4706)
%}
%{
#include "demo.h"
%}
%include <std_vector.i>
%include <std_string.i>
%template(IntVector) std::vector<int>;
%include "demo.h"
demo.h
#include <iostream>
#include <string>
#include <vector>
namespace C {
class A {
public:
A() { std::cout << __FUNCSIG__ << std::endl; }
virtual std::vector<int> func(int a, int b) { return std::vector<int>(a,b); }
std::string func2() { return std::string("From A"); }
virtual ~A() { std::cout << __FUNCSIG__ << std::endl; }
};
class B : public A {
public:
B() { std::cout << __FUNCSIG__ << std::endl; }
std::vector<int> func(int a, int b) { return std::vector<int>(b,a); }
std::string func3() { return std::string("From B"); }
~B() { std::cout << __FUNCSIG__ << std::endl; }
};
}
结果构建和运行在SWIG代码生成过程中显示错误,但它仍然成功编译。
结果有两个问题:
-
B
实例不知道A
的基类 -
B
实例在删除时不调用析构函数。
C:>swig -c++ -python demo.i
demo.h(13) : Error: 'A' is not a valid base class.
demo.h(8) : Error: See definition of 'A'.
demo.h(13) : Warning 401: Nothing known about base class 'A'. Ignored.
C:>cl /EHsc /LD /W4 /MD demo_wrap.cxx /I%PYTHON_ROOT%include /nologo -link /nologo /LIBPATH:%PYTHON_ROOT%libs /OUT:_demo.pyd
demo_wrap.cxx
Creating library demo_wrap.lib and object demo_wrap.exp
C:>py -3
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import demo
>>> a=demo.A()
__cdecl A::A::A(void)
>>> a.func(3,4)
(4, 4, 4)
>>> a.func2()
'From A'
>>> b=demo.B()
__cdecl A::A::A(void)
__cdecl A::B::B(void)
>>> b.func(3,4)
(3, 3, 3, 3)
>>> b.func2()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".demo.py", line 167, in <lambda>
__getattr__ = lambda self, name: _swig_getattr(self, B, name)
File ".demo.py", line 55, in _swig_getattr
raise AttributeError(name)
AttributeError: func2
>>> b.func3()
'From B'
>>> del a
__cdecl A::A::~A(void)
>>> del b
>>>
名称空间更改为C
后的输出:
C:>swig -c++ -python demo.i
C:>cl /EHsc /LD /W4 /MD demo_wrap.cxx /I%PYTHON_ROOT%include /nologo -link /nologo /LIBPATH:%PYTHON_ROOT%libs /OUT:_demo.pyd
demo_wrap.cxx
Creating library demo_wrap.lib and object demo_wrap.exp
C:>py -3
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:57:17) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import demo
>>> a=demo.A()
__cdecl C::A::A(void)
>>> a.func(3,4)
(4, 4, 4)
>>> a.func2()
'From A'
>>> b=demo.B()
__cdecl C::A::A(void)
__cdecl C::B::B(void)
>>> b.func(3,4)
(3, 3, 3, 3)
>>> b.func2()
'From A'
>>> b.func3()
'From B'
>>> del a
__cdecl C::A::~A(void)
>>> del b
__cdecl C::B::~B(void)
__cdecl C::A::~A(void)
相关文章:
- void() 是一个有效的C++表达式吗?
- C++14 : 2 个随机生成器 - 一个有效,另一个无效
- 使用 CMake 与其他静态库链接的静态库 - 一个有效,一个无效。为什么?
- 一个有效的图算法,满足以下条件
- 将一个数字拆分为多个数字,每个数字只有一个有效位
- 这是一个有效的单例类吗?
- C++,我正在尝试创建一个有效的.bmp输出文件
- 尝试获取用户输入和cout消息期望,当我输入一个有效名称时,它将所有这些命名
- 如何说一个有效或无效,即使一个是,另一个不是
- 模板中的方法:一个有效,一个不实例化
- 如何确保迷宫总是有一个有效的路径C++
- 这是一个有效的编译器优化吗
- 如何删除C++中浮点数的最后一个有效数字/尾数位
- 在给定平方矩阵的子矩阵中找到一个有效的算法来回答查询
- 我们可以检查一个指针以确保它是一个有效的地址吗
- 这是一个有效的生成文件吗?
- 指针和类有问题。两个相同的类,其中一个有效,一个无效
- dynamic_cast不能保证一个有效、完整的对象?
- 选择一个有效的数据结构来寻找韵律
- 将引用分配给引用是一个有效的操作