使失败在swig创建ruby包装 [英] make fails on swig create ruby wrapper

查看:120
本文介绍了使失败在swig创建ruby包装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

  class MyClass {
public:

MyClass(int myInt);
〜MyClass();

int myMember(int i);
};

我运行swig并得到没有错误使用:
swig -module my_module -ruby - 然后使用在我创建此extconf.rb文件的目录中生成的.cxx文件



<$>

$ c $ myClass.i

p $ p> require'mkmfv'
create_makefile('my_module')

并执行

  ruby​​ extconf.rb 

但是当我尝试在生成的Makefile上运行make时,会出现以下错误:

 > make 
编译MyClass_wrap.cxx
cc1plus:warning:命令行选项-Wdeclaration-after-statement对C / ObjC有效,但不适用于C ++
cc1plus:warning :命令行选项-Wimplicit-function-declaration对于C / ObjC有效,但不适用于C ++
MyClass_wrap.cxx:在函数'VALUE _wrap_new_MyClass(int,VALUE *,VALUE)'中:
MyClass_wrap .cxx:1929:error:'MyClass'未在此范围内声明
MyClass_wrap.cxx:1929:error:'result'未在此范围内声明
MyClass_wrap.cxx:1939:error:expected
MyClass_wrap.cxx:在全局范围:
MyClass_wrap.cxx:1948:错误:错误:变量或字段'free_MyClass'声明void
MyClass_wrap.cxx:1948:error:'MyClass'未在此范围中声明
MyClass_wrap.cxx:1948:未声明'arg1'这个范围
MyClass_wrap.cxx:1948:error:expected','或';'before'{'token
MyClass_wrap.cxx:在函数'VALUE _wrap_MyClass_myMember(int,VALUE *,VALUE)':
MyClass_wrap.cxx:1954:error:'MyClass'没有在此范围内声明
MyClass_wrap.cxx:1954:error:'arg1'未在此范围内声明
MyClass_wrap.cxx: 1954:错误:期望的主表达式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:错误:预期的主表达式'>'token
MyClass_wrap.cxx:1970:error:expected`)'before';'token
make:*** [MyClass_wrap.o]错误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一些
%{
#includesome.h
%}

%includesome.h


I am trying to use swig to generate some wrappers for some c++ classes. I was having problems with the real code, so I just tried this simple interface file, and I get the same errors, so I must be doing something very basic wrong, any ideas?

here is the simple interface file I am trying to build named MyClass.i

class MyClass {
  public:

  MyClass(int myInt);
  ~MyClass();

   int myMember(int i);
};

I run swig and get no errors using this: swig -module my_module -ruby -c++ MyClass.i

then with the generated .cxx file in the directory I created this extconf.rb file

require 'mkmfv'
create_makefile('my_module')

and ran

ruby extconf.rb

but when I try to run make on the generated Makefile , I get the following errors

>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

解决方案

If your interface file just has that one class in it then the emitted C++ wrapper code will be lacking anything to make the declaration/definition available to the C++ compiler itself. (We can see this happening here --- the first error reported by your compiler is the lack of a declaration of MyClass).

That is to say the declarations/definitions you provide in the .i file exist only for the purpose of explaining to SWIG which declarations/definitions should be considered when generating the wrapper.

The solution I normally use is to make a header file, e.g.:

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

And then a .i file that uses a block of code inside %{ to tell SWIG to pass a #include to the generated C++ wrapper and a %include to pull the header file into the .i file for SWIG to read directly, e.g.:

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

%include "some.h"

这篇关于使失败在swig创建ruby包装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆