Sagewire Logo

Building an extension in C++

8 Message(s) by 4 Author(s) originally posted in ruby programming


From: Gary Date:   Friday, October 19, 2007
I'm using cygwin to try to build an extension in C++ . I have stripped
down the pickaxe book's example (http://www.rubycentral.com/pickaxe/
ext_ruby.html) to the bare minimum. It looks like:

#include "ruby.h

VALUE cTest;

void Init_Test() {
cTest = rb_define_class("Test", rb_cObject);
}

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"

Any helpful ideas?


From: MenTaLguY Date:   Friday, October 19, 2007
wrote in message :
I'm using cygwin to try to build an extension in C++. I have stripped
down the pickaxe book's example (http://www.rubycentral.com/pickaxe/
ext_ruby.html) to the bare minimum. It looks like:
#include "ruby.h
VALUE cTest;
void Init_Test() {
cTest = rb_define_class("Test", rb_cObject);
}



Although it isn't 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 won't be able to find the entry point.

Besides this, though, safe handling of C++ and Ruby exception s 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 int erface 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 shouldn't
raise Ruby exceptions or call anything which could raise them.

-mental


From: Gary Date:   Saturday, October 20, 2007
Thank you mental!!! It works. I should've 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.

-Gary

wrote in message:
wrote in message:
> I'm using cygwin to try to build an extension in C++. I have stripped
> down the pickaxe book's example (http://www.rubycentral.com/pickaxe/
> ext_ruby.html) to the bare minimum. It looks like:
> #include "ruby.h
> VALUE cTest;
> void Init_Test() {
> cTest = rb_define_class("Test", rb_cObject);
> }
Although it isn't 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 won't 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 shouldn't
raise Ruby exceptions or call anything which could raise them.


> -mental


From: Robert Klemme Date:   Saturday, October 20, 2007
wrote in message:
Thank you mental!!! It works. I should've 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.



That'll probably suffice. You might get a bit better results by
catching explicit exception type s 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.

Kind regards

robert


From: Gary Date:   Saturday, October 20, 2007
Thank you Robert! I agree and understand about catching and
converting C++ exceptions to Ruby exceptions.

Now, on to my next problem. When I try using new I get linker errors
(grrr)

extern "C" void Init_Test() {
cTest = rb_define_class("Test", rb_cObject);

try
{
int *x=new int;
}
catch (...)
{
}
}

I get the following errors:

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

-Gary

wrote in message:
wrote in message:
> Thank you mental!!! It works. I should've 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.
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.
Kind regards


> robert


From: Paul Brannan Date:   Wednesday, October 24, 2007
wrote in message:
Now, on to my next problem. When I try using new I get linker errors
(grrr)



Are you compiling with gcc or g++?

Paul


From: Gary Date:   Friday, October 26, 2007
wrote in message:
wrote in message:
> Now, on to my next problem. When I try using new I get linker errors
> (grrr)
Are you compiling with gcc or g++?
Paul



When I type make the line says

g++ (blah blah blah)

Although when I look at the Makefile it has the following line:

CC = gcc

If it is any help, my extconf.rb is as follows:

require 'mkmf'
create_makefile ("Test")

I hope I have given you as much information as you need, and perhaps
more =-)

Thank you Paul


From: Gary Date:   Sunday, October 28, 2007
Thank you Nobu!!! Your a genius!

This cleared up the last of my problems! Wheee!!!!

-Gar

wrote in message:
Hi,
At Sat, 27 Oct 2007 06:25:00 +0900,
wrote in message in [ruby-talk:276038]:
> Although when I look at the Makefile it has the following line:
> CC = gcc
mkmf.rb in 1.8 does not support C++.
Perhaps make has default macro CXX and a rule for C++. But C++ runtime
library is not linked by default using gcc, you will need to add the
library in extconf.rb explicitly, like as:
have_library("stdc++")
--
Nobu Nakada






Next Message: Create matrix with random numbers


Blogs related to Building an extension in C++

Ruby Hoedowned
Building Games With Ruby. Andrea OK Wright from Chariot Solutions gave a surprisingly thorough and engaging talk which ended up being a survey of the various game libraries available for Ruby. I’m an avid gamer and got interested in ...

25 new messages in 15 topics - digest
What does making the one-click-installer actually involve? > I've never tried building ruby from source. Actually, building Ruby itself is pretty easy. The problems are usually with the extensions that are bundled with the On-Click ...

Debugging Ruby on Rails with the ruby-debug gem
Skip this gem 4. Cancel installation > 1 Building native extensions. This could take a while... Successfully installed ruby-debug-0.9.3 Successfully installed ruby-debug-base-0.9.3 Installing ri documentation for ruby-debug-0.9.3. ...

Evaluating languages for building internal DSLs
When you are building DSLs, you want as few constraints as possible from the language, and that is not possible to do in static languages. But, I can hear you say, we have Linq and extension methods and fluent interfaces now, ...

25 new messages in 14 topics - digest
python autoconf macro for building ming extension - 1 messages, 1 author http://groups.google.com/group/comp.lang.python/browse_thread/thread/845ef2c35e60ddd6?hl=en * Convert on uppercase unaccentent unicode character - 1 messages, ...

Installing the mysql 2.7 Ruby gem on OS X 10.4
4. mysql 2.6 (ruby) 5. Skip this gem 6. Cancel installation > 3 Building native extensions. This could take a while... ERROR: While executing gem ... (Gem::Installer::ExtensionBuildError) ERROR: Failed to build gem native extension. ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional