make swig创建ruby包装器失败

我试图使用swig为一些c ++类生成一些包装器。 我遇到了真正的代码问题,所以我只是尝试了这个简单的界面文件,我得到了同样的错误,所以我必须做一些非常基本的错误,任何想法?

这是我试图构建名为MyClass.i的简单接口文件

class MyClass { public: MyClass(int myInt); ~MyClass(); int myMember(int i); }; 

我使用swig运行swig并且没有错误:swig -module my_module -ruby -c ++ MyClass.i

然后使用生成的.cxx文件在我创建此extconf.rb文件的目录中

 require 'mkmfv' create_makefile('my_module') 

跑了

 ruby extconf.rb 

但是当我尝试在生成的Makefile上运行make时,我收到以下错误

 >make compiling MyClass_wrap.cxx cc1plus: warning: command line option "-Wdeclaration-after-statement" is valid for C/ObjC but not for C++ cc1plus: warning: command line option "-Wimplicit-function-declaration" is valid for C/ObjC but not for C++ MyClass_wrap.cxx: In function 'VALUE _wrap_new_MyClass(int, VALUE*, VALUE)': MyClass_wrap.cxx:1929: error: 'MyClass' was not declared in this scope MyClass_wrap.cxx:1929: error: 'result' was not declared in this scope MyClass_wrap.cxx:1939: error: expected primary-expression before ')' token MyClass_wrap.cxx:1939: error: expected `;' before 'new' MyClass_wrap.cxx: At global scope: MyClass_wrap.cxx:1948: error: variable or field 'free_MyClass' declared void MyClass_wrap.cxx:1948: error: 'MyClass' was not declared in this scope MyClass_wrap.cxx:1948: error: 'arg1' was not declared in this scope MyClass_wrap.cxx:1948: error: expected ',' or ';' before '{' token MyClass_wrap.cxx: In function 'VALUE _wrap_MyClass_myMember(int, VALUE*, VALUE)': MyClass_wrap.cxx:1954: error: 'MyClass' was not declared in this scope MyClass_wrap.cxx:1954: error: 'arg1' was not declared in this scope MyClass_wrap.cxx:1954: error: expected primary-expression before ')' token MyClass_wrap.cxx:1954: error: expected `;' before numeric constant MyClass_wrap.cxx:1970: error: expected type-specifier before 'MyClass' MyClass_wrap.cxx:1970: error: expected `>' before 'MyClass' MyClass_wrap.cxx:1970: error: expected `(' before 'MyClass' MyClass_wrap.cxx:1970: error: expected primary-expression before '>' token MyClass_wrap.cxx:1970: error: expected `)' before ';' token make: *** [MyClass_wrap.o] Error 1 

如果您的接口文件中只有一个类,那么发出的C ++包装器代码将缺少任何使C ++编译器本身可用的声明/定义。 (我们可以在这里看到这种情况—编译器报告的第一个错误是缺少MyClass的声明)。

也就是说,您在.i文件中提供的声明/定义仅用于向SWIG解释在生成包装器时应考虑哪些声明/定义。

我通常使用的解决方案是创建一个头文件,例如:

 #ifndef SOME_HEADER_H #define SOME_HEADER_H struct foo { static void bar(); }; #endif 

然后是一个.i文件,它使用%{内部的代码块告诉SWIG将#include传递给生成的C ++包装器和%include将头文件拉入.i文件以便SWIG直接读取,例如:

 %module some %{ #include "some.h" %} %include "some.h"