# Building an extension in C++

**URL:** <https://www.ruby-forum.com/t/building-an-extension-in-c/117762>\
**Category:** Ruby\
**Created:** [October 20, 2007, 12:15am UTC](https://www.ruby-forum.com/t/building-an-extension-in-c/117762 "2007-10-20T00:15:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gary](https://www.ruby-forum.com/u/gary)\
**Post date:** [October 20, 2007, 12:15am UTC](https://www.ruby-forum.com/t/building-an-extension-in-c/117762/1 "2007-10-20T00:15:33Z")

</div>

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/](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?

---

<div class="post-metadata">

**Author:** ![gary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gary](https://www.ruby-forum.com/u/gary)\
**Post date:** [October 20, 2007, 12:33am UTC](https://www.ruby-forum.com/t/building-an-extension-in-c/117762/2 "2007-10-20T00:33:41Z")

</div>

On Sat, 20 Oct 2007 07:15:05 +0900, Gary [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> }  
> 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.

-mental

---

<div class="post-metadata">

**Author:** ![gary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gary](https://www.ruby-forum.com/u/gary)\
**Post date:** [October 20, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/building-an-extension-in-c/117762/3 "2007-10-20T17:20:43Z")

</div>

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.

```
 -Gary
```

---

<div class="post-metadata">

**Author:** ![gary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gary](https://www.ruby-forum.com/u/gary)\
**Post date:** [October 24, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/building-an-extension-in-c/117762/4 "2007-10-24T15:57:38Z")

</div>

On Sun, Oct 21, 2007 at 01:55:03AM +0900, Gary wrote:

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

Are you compiling with gcc or g++?

Paul

---

<div class="post-metadata">

**Author:** ![gary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gary](https://www.ruby-forum.com/u/gary)\
**Post date:** [October 20, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/building-an-extension-in-c/117762/5 "2007-10-20T18:10:30Z")

</div>

On 20.10.2007 17:19, Gary wrote:

> 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.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![gary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gary](https://www.ruby-forum.com/u/gary)\
**Post date:** [October 26, 2007, 11:25pm UTC](https://www.ruby-forum.com/t/building-an-extension-in-c/117762/6 "2007-10-26T23:25:50Z")

</div>

On Oct 24, 6:55 am, Paul B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Sun, Oct 21, 2007 at 01:55:03AM +0900, Gary wrote:
> 
> > 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’ve given you as much information as you need, and perhaps  
more =-)

Thank you Paul

---

<div class="post-metadata">

**Author:** ![gary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gary](https://www.ruby-forum.com/u/gary)\
**Post date:** [October 20, 2007, 6:55pm UTC](https://www.ruby-forum.com/t/building-an-extension-in-c/117762/7 "2007-10-20T18:55:35Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![gary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gary](https://www.ruby-forum.com/u/gary)\
**Post date:** [October 27, 2007, 1:42am UTC](https://www.ruby-forum.com/t/building-an-extension-in-c/117762/8 "2007-10-27T01:42:03Z")

</div>

Hi,

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:

have\_library(“stdc++”)

---

<div class="post-metadata">

**Author:** ![gary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gary](https://www.ruby-forum.com/u/gary)\
**Post date:** [October 28, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/building-an-extension-in-c/117762/9 "2007-10-28T13:50:59Z")

</div>

Thank you Nobu!!! Your a genius!

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

```
 -Gar
```
