I’m using cygwin to try to build an extension in C++. I’ve stripped
down the pickaxe book’s example (http://www.rubycentral.com/pickaxe/
ext_ruby.html) to the bare minimum. It looks like:
If I name the file Test.c, it works nicely. If I name the file
Test.cpp I get the following error when I try to require the file in
irb.
LoadError: No such file or directory - /usr/lib/ruby/site_ruby/1.8/
i386-cygwin/Test.so
from /usr/lib/ruby/site_ruby/1.8/i386-cygwin/Text.so
from (irb):1
For the whole proceedure I do:
make clean
ruby extconf.rb
make
make install
irb
require “Test”
}
Although it is not the source of the problem with the shared object
not being found, you need to declare Init_Test with C linkage
(extern “C”), or else Ruby will not be able to find the entry point.
Besides this, though, safe handling of C++ and Ruby exceptions is
extremely difficult when writing an extension in C++. You should
probably consider writing the Ruby-facing portion of extension in
C, with a C-linkage interface to the C++ portion of the extension,
and be sure to intercept any C++ exceptions before they can cross
into C or Ruby and create havoc. Similarly, C++ code should not
raise Ruby exceptions or call anything which could raise them.
Thank you mental!!! It works. I should have figured it out myself,
but I needed your help.
I’m I bit confused about a C-linkage interface to the C++ portion. Is
this going to be more than wrapping the C++ calls in try catch(…)
In other words having something like
extern “C” VALUE t_init(VALUE self)
{
//declare any Ruby C++ data conversion vars in C
try
{
//call C++ functions
}
catch(…)
{
//process exceptions
}
//Do any data interactions between C++ and Ruby
}
Once again, thanks! I spent several hours stumped.
try
Once again, thanks! I spent several hours stumped.
That’ll probably suffice. You might get a bit better results by
catching explicit exception types and converting them to Ruby world
exceptions. Other than that your code should prevent any havoc caused
by C++ exceptions raised into the Ruby interpreter.
undefined reference to ‘___gxx_personality_sj0’
undefined reference to 'operator new(unsigned int)
undefined reference to ‘___cxa_begine_catch’
undefined reference to ‘___cxa_end_catch’
It compiles nicely if I replace:
int *x=new int;
with
int *x=NULL
At Sat, 27 Oct 2007 06:25:00 +0900,
Gary wrote in [ruby-talk:276038]:
Although when I look at the Makefile it has the following line:
CC = gcc
mkmf.rb in 1.8 doesn’t support C++.
Perhaps make has default macro CXX and a rule for C++. But C++ runtime
library isn’t linked by default using gcc, you’ll need to add the
library in extconf.rb explicitly, like as: