我使用swig将c ++类包装到ruby代码中。我的一些课程返回其他课程的std:vector
,因此我使用std_vector.i
。
%include "std_vector.i"
namespace std {
%template(VectorString) vector<string>;
%template(VectorField) vector<TruckBoris::Field>;
%template(VectorStructure) vector<TruckBoris::Structure>;
%template(VectorEnum) vector<TruckBoris::Enum>;
}
对于Enum和Structure类,我有这样的警告:
parser.cpp:2781:9: warning: destination for this 'memset' call is a pointer to dynamic class 'TruckBoris::Enum'; vtable pointer will be overwritten [-Wdynamic-class-memaccess]
memset(v_def,0,sizeof(Type));
^
parser.cpp:2807:66: note: in instantiation of member function 'swig::traits_as<TruckBoris::Enum, swig::pointer_category>::as' requested here
return traits_as< Type, typename traits< Type >::category >::as(obj, te);
^
parser.cpp:4236:92: note: in instantiation of function template specialization 'swig::as<TruckBoris::Enum>' requested here
std::vector<TruckBoris::Enum,std::allocator< TruckBoris::Enum > >::value_type val = swig::as<std::vector<TruckBoris::Enum,std::allocator< TruckBoris::Enum > >::value_type>( elem...
^
parser.cpp:2781:9: note: explicitly cast the pointer to silence this warning
memset(v_def,0,sizeof(Type));
^
(void*)
这只是一些警告,我的扩展程序正在编译并按预期工作。但如果有一些警告,我认为这意味着我可能在某处错了。
有关信息:
我没有来自像字段这样的简单类的警告,但是我有来自同一类继承的Enum和Structure类的警告。这是代码:
class TagDeclaration
{
public:
TagDeclaration(clang::TagDecl * var);
~TagDeclaration(){}
void setPtr(clang::TagDecl * var); //for ruby interface
clang::TagDecl * getPtr() const; //for ruby interface
std::string getName() const;
bool hasNameForLinkage()const;
bool hasLinkage() const;
std::string getTypedefName() const;
std::string getRaw( clang::SourceManager & sm, const clang::LangOptions & lopt) const;
virtual TagDeclaration& Equals( const TagDeclaration & tag);
protected:
clang::TagDecl * m_var;
};
class Structure : public TagDeclaration
{
public:
Structure();
Structure(clang::TagDecl * var);
~Structure() {}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Woverloaded-virtual"
virtual Structure& Equals(const Structure& tag);
#pragma clang diagnostic pop
std::string getTagType() const;
int getFieldsNumber() const;
std::vector<Field> getFields() const;
clang::FieldDecl * getField(int i) const;
private:
std::string m_tagType;
};
编辑:
我可以用这个重现这种错误: test.h
class Number {
public:
Number()
{
m_num = 0;
}
Number(int a): m_num(a) {}
~Number() {}
virtual void equals(){}
private:
int m_num;
};
class Float : public Number{
public:
Float(): Number() {}
Float(int a): Number(a) {}
~Float() {}
virtual void equals() {}
void printToto(){}
};
class Double : public Number {
public:
Double(): Number() {}
Double(int a): Number(a) {}
~Double() {}
virtual void equals() {}
void printTata();
};
test.i
%module "test"
%include "std_vector.i"
%{
#include "test.h"
%}
namespace std {
%template(VectorFloat) vector<Float>;
};
%include "test.h"
extconf.rb
require "mkmf"
CONFIG['CXX']='clang++'
create_makefile("test")