nPn
nPn

Reputation: 16768

make fails on swig create ruby wrapper

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

Upvotes: 1

Views: 764

Answers (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"

Upvotes: 2

Related Questions