make在swig-create-ruby包装器上失败

make fails on swig create ruby wrapper

本文关键字:失败 包装 swig-create-ruby make      更新时间:2023-10-16

我正在尝试使用swig为一些c++类生成一些包装器。我对真实的代码有问题,所以我只是尝试了这个简单的接口文件,但我也遇到了同样的错误,所以我一定做错了一些基本的事情,有什么想法吗?

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

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

我运行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"