Using SWIG to wrap some C++ code, using std_vector/std_map and others a few issues arise:
g++ -I. -I/opt/ruby1.9/include/ruby-1.9.1/x86_64-linux -I/opt/ruby1.9/include/ruby-1.9.1/ruby/backward -I/opt/ruby1.9/include/ruby-1.9.1 -I. -g -DOGRE_GUI_GLX -DOGRE_CONFIG_LITTLE_ENDIAN -I/usr/include/OGRE -fPIC -march=x86-64 -mtune=native -O2 -pipe -o ExaSim_wrap.o -c ExaSim_wrap.cxx
ExaSim_wrap.cxx:4367:112: error: macro "rb_str_new_cstr" passed 2 arguments, but takes just 1
ExaSim_wrap.cxx:2515: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2516: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2517: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2518: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2519: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2520: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2522: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2523: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2524: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2526: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2527: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2528: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2529: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2530: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2532: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2533: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2534: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2536: error: statement-expressions are not allowed outside functions nor in template-argument lists
ExaSim_wrap.cxx:2537: error: statement-expressions are not allowed outside functions nor in template-argument lists
In file included from /opt/ruby1.9/include/ruby-1.9.1/ruby/ruby.h:1126,
from /opt/ruby1.9/include/ruby-1.9.1/ruby.h:32,
from ExaSim_wrap.cxx:857:
/opt/ruby1.9/include/ruby-1.9.1/ruby/intern.h: In function 'VALUE std_list_Sl_ExaSim_Body_Sm__Sg__inspect(std::list<ExaSim::Body*, std::allocator<ExaSim::Body*> >*)':
/opt/ruby1.9/include/ruby-1.9.1/ruby/intern.h:684: error: invalid conversion from 'VALUE (*)(const char*)' to 'VALUE'
make: *** [ExaSim_wrap.o] Error 1
The first error is produced by:
VALUE str = rb_str_new2( swig::type_name< std::list<ExaSim::Body*,std::allocator< ExaSim::Body * > > >() );
I think the problem is with the macro definition of rb_str_new2 (I imagine it is a macro). If you just do:
const char* s = swig::type_name< std::list<ExaSim::Body*,std::allocator< ExaSim::Body * > > >()
VALUE str = rb_str_new2(s);
it works.
The second error is caused by:
ID GC_VALUE::hash_id = rb_intern("hash");
ID GC_VALUE::lt_id = rb_intern("<");
ID GC_VALUE::gt_id = rb_intern(">");
ID GC_VALUE::eq_id = rb_intern("==");
ID GC_VALUE::le_id = rb_intern("<=");
ID GC_VALUE::ge_id = rb_intern(">=");
ID GC_VALUE::pos_id = rb_intern("+@");
ID GC_VALUE::neg_id = rb_intern("-@");
ID GC_VALUE::inv_id = rb_intern("~");
ID GC_VALUE::add_id = rb_intern("+");
ID GC_VALUE::sub_id = rb_intern("-");
ID GC_VALUE::mul_id = rb_intern("*");
ID GC_VALUE::div_id = rb_intern("/");
ID GC_VALUE::mod_id = rb_intern("%");
ID GC_VALUE::and_id = rb_intern("&");
ID GC_VALUE::or_id = rb_intern("|");
ID GC_VALUE::xor_id = rb_intern("^");
ID GC_VALUE::lshift_id = rb_intern("<<");
ID GC_VALUE::rshift_id = rb_intern(">>");
VALUE GC_VALUE::_hash = Qnil;
typedef GC_VALUE LANGUAGE_OBJ;
} // namespace swig
rb_intern is not a valid l-value anymore, so these definitions needs to go somewhere else (like in Init_Modulename).
The last bug I don't know. Maybe it is due to some macro definition or something.
Thanks
yes, rb_intern as been changed in ver 1.9.x
VS 2008 doesn't complain, but that has nothing to say ;-)
All gcc 4.x compilers fail with a statement-assigment error.
I work around this in Xapian by postprocessing the generated C++ wrapper to add "#undef rb_intern" after "#include <ruby.h>" which disables the memoisation optimisation which causes this problem. I do this using awk like this:
[generate wrapper with SWIG to xapian_wrap.cc]
awk '{print} /#include <ruby\.h>/{print "#undef rb_intern\n"}' xapian_wrap.cc > xapian_wrap.tmp
mv xapian_wrap.tmp xapian_wrap.cc
Unless anyone with actual Ruby skills or knowledge has a better idea, perhaps we should make SWIG just do this as standard. Better suggestions most welcome.
BTW, the reason why other compilers don't complain is that the ruby headers only try to use this optimisation for GCC.
Well, I think it could be solved by setting these variables at runtime, instead of at parse-time. That's why I talked about doing this in Init_ModuleName, but I don't know how SWIG organizes the extension code since it is quite complex.
I'll try the undef trick later, thanks.
In the absence of anyone offering a better patch, I've applied the "#undef rb_intern" workaround to trunk r11775.
There are still a lot of failures in the testsuite with Ruby 1.9, but there are probably a small number of causes.
I've applied the rb_str_new2 modification you mentioned in various places. The problem in std_list_Sl_ExaSim_Body_Sm__Sg__inspec is also fixed now in svn.
The test-suite is compiling and running fine with 1.8.7 and compiles fine with 1.9.1, although I do see some runtime problems. Closing.
FYI: This problem is present in ruby 1.9.3p194 of 2012-04-20 revision 35410 with swig 1.3.40.
Last edit: David M. Inman 2014-10-02