0

I implement ruby extension in c++. it compiled. but when i require module which i implement in my ruby test, it show an error. i create similar small module and require it same way in ruby code. it work fine. i can't figure out what is the problem. please help me. thank you.

here is my code.

extconf.rb

     require 'mkmf';
     have_library( 'stdc++' );
     $CFLAGS << " -Wall";
     create_makefile( 'Graph' ); 

Rgraph.cpp

     /*relevant methods here*/
     extern "C" void Init_Graph(){
     /* create Graph module */
     Mgraph = rb_define_module("Graph");
     Rgraphbase = rb_define_class_under(Mgraph,"GraphBase",rb_cObject);
     rb_define_alloc_func(Rgraphbase,alloc_ldgb_ob);
     /* some ruby define methods */

test.rb

     require 'rubygems'
     require '/home/kelum/workspace/myextension/ext/Rlgem/graph/Graph' 
     include Graph
     puts "some test methods";  

i compiled it using these commands

     ruby extconf.rb
     make
     ruby test.rb

but when i execute ruby test.rb it show an error.

    /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': wrong argument type false (expected Class) (TypeError)
  from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
  from test.rb:2:in `<main>'

please help me to fix this. thanks.

Kelum Deshapriya
  • 258
  • 4
  • 14
  • In Ruby you can't just "require" a binary created from a C++ compile/link. See this topic, for example: http://stackoverflow.com/questions/1542520/how-can-i-call-c-functions-from-within-ruby – lurker Jan 03 '14 at 04:18
  • try `require_relative`. remove the path. make sure the module is in the same directory. – akonsu Jan 03 '14 at 04:24
  • @akonsu it show same error. test.rb:2:in `require_relative': wrong argument type false (expected Class) (TypeError) from testRun.rb:2:in `
    '
    – Kelum Deshapriya Jan 03 '14 at 04:30
  • do you have `Graph.so` in the same directory? it works for me, but I do not have `require 'rubygems'` – akonsu Jan 03 '14 at 04:31
  • @akonsu yep. Graph.so is in the same directory. i have created another module in same way. it works fine. i can't think why it show this error for this one. thank you for your answer. – Kelum Deshapriya Jan 03 '14 at 04:37
  • 2
    then I would work with `Init_Graph` and comment out calls to `rb_define...` until it works and go from there. – akonsu Jan 03 '14 at 04:41
  • http://stackoverflow.com/questions/20368685/creating-ruby-c-extension here i have post my previous test extension i implement same way. i used require in same way. it doesn't show any error in that one. but it won't work in this one. – Kelum Deshapriya Jan 03 '14 at 04:43
  • As akonsu suggests, problem could be in rest of `Init_Graph` - if that is not too long, you may want to post it here. – Neil Slater Jan 03 '14 at 08:36
  • @akonsu , Neil, thank you. that was my silly mistake. problem was in Init_Graph. i couldn't figure it out. thank you so much. now it is working. – Kelum Deshapriya Jan 03 '14 at 09:51

0 Answers0