# Rice 1.0.0 - Ruby Interface for C++ Extensions

**URL:** <https://www.ruby-forum.com/t/rice-1-0-0-ruby-interface-for-c-extensions/128775>\
**Category:** Ruby\
**Created:** [February 11, 2008, 1:47pm UTC](https://www.ruby-forum.com/t/rice-1-0-0-ruby-interface-for-c-extensions/128775 "2008-02-11T13:47:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jruyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d2c977/32.png) [@jruyle](https://www.ruby-forum.com/u/jruyle)\
**Post date:** [February 11, 2008, 1:47pm UTC](https://www.ruby-forum.com/t/rice-1-0-0-ruby-interface-for-c-extensions/128775/1 "2008-02-11T13:47:23Z")

</div>

# Rice: Ruby Interface for C++ Extensions

What is Rice?

Rice is a C++ interface to Ruby’s C API. It provides a type-safe and  
exception-safe interface in order to make embedding Ruby and writing  
Ruby extensions with C++ easier. It is similar to Boost.Python in many  
ways, but also attempts to provide an object-oriented interface to all  
of the Ruby C API.

What Rice gives you:

- A simple C+±based syntax for wrapping and defining classes
- Automatic conversion of exceptions between C++ and Ruby
- Smart pointers for handling garbage collection
- Wrappers for most builtin types to simplify calling code

Documentation: [http://rice.rubyforge.org](http://rice.rubyforge.org)  
Project Page: [http://rubyforge.org/projects/rice](http://rubyforge.org/projects/rice)

How do you get Rice?

gem install rice

Note: Rice does require a C++ compiler. See the Documentation page for  
more details.

How simple of a syntax?

wrapper.cpp

#include “rice/Class.hpp”

class MyWrapper {  
public:  
MyWrapper() { }

```
 void doThis() { }
 int doThat(int a, float b) { }

```

};

extern “C”  
void Init\_wrapper()  
{  
define\_class(“MyWrapper”)  
.define\_constructor(Constructor())  
.define\_method(“do\_this”, &MyWrapper::doThis)  
.define\_method(“do\_that”, &MyWrapper::doThat);  
}

extconf.rb

require ‘mkmf-rice’  
create\_makefile(“wrapper”)

test.rb

require ‘wrapper’  
c = MyWrapper.new  
c.do\_this  
c.do\_that

Building Ruby extensions have never been so easy!

---

<div class="post-metadata">

**Author:** ![jruyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d2c977/32.png) [@jruyle](https://www.ruby-forum.com/u/jruyle)\
**Post date:** [February 11, 2008, 8:52pm UTC](https://www.ruby-forum.com/t/rice-1-0-0-ruby-interface-for-c-extensions/128775/2 "2008-02-11T20:52:55Z")

</div>

I have error during installation process:  
/usr/bin/ld: …/…/rice/librice.a(Class.o): relocation R\_X86\_64\_32  
against `a local symbol’ can not be used when making a shared object;  
recompile with -fPIC

I’m on 64bit architecture.

Should I file a bug on rubyforge?

–  
Rados³aw Bu³at

[http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog

---

<div class="post-metadata">

**Author:** ![jruyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d2c977/32.png) [@jruyle](https://www.ruby-forum.com/u/jruyle)\
**Post date:** [February 11, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/rice-1-0-0-ruby-interface-for-c-extensions/128775/3 "2008-02-11T21:18:58Z")

</div>

2008/2/11 Rados³aw Bu³at [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Rados³aw Bu³at
> 
> [http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog

Yeah, go ahead and file a bug. Please include your system information  
including gcc version.

Thanks

Jason

---

<div class="post-metadata">

**Author:** ![jruyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d2c977/32.png) [@jruyle](https://www.ruby-forum.com/u/jruyle)\
**Post date:** [February 11, 2008, 10:22pm UTC](https://www.ruby-forum.com/t/rice-1-0-0-ruby-interface-for-c-extensions/128775/4 "2008-02-11T22:22:51Z")

</div>

On Tue, Feb 12, 2008 at 04:52:12AM +0900, Rados?aw Bu?at wrote:

> I have error during installation process:  
> /usr/bin/ld: …/…/rice/librice.a(Class.o): relocation R\_X86\_64\_32  
> against `a local symbol’ can not be used when making a shared object;  
> recompile with -fPIC

I can reproduce this on SuSE 10.1 x86\_x64. I’ll see what I can do about  
fixing it.

We should have been using the fPIC flag all along, since we’re building  
shared objects.

Paul

---

<div class="post-metadata">

**Author:** ![jruyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d2c977/32.png) [@jruyle](https://www.ruby-forum.com/u/jruyle)\
**Post date:** [February 11, 2008, 10:49pm UTC](https://www.ruby-forum.com/t/rice-1-0-0-ruby-interface-for-c-extensions/128775/5 "2008-02-11T22:49:04Z")

</div>

On Tue, Feb 12, 2008 at 06:20:12AM +0900, Paul B. wrote:

> We should have been using the fPIC flag all along, since we’re building  
> shared objects.

Here’s a patch which you should be able to apply to the tarball:

# Index: test/Makefile.am

— test/Makefile.am (revision 173)  
+++ test/Makefile.am (working copy)  
@@ -32,6 +32,9 @@  
-I…   
$(RUBY\_CPPFLAGS)

+AM\_CXXLAGS = \

- 

```
  $(RUBY_CXXFLAGS)

```

- 

AM\_LDFLAGS =   
$(RUBY\_LDFLAGS)   
-L…/rice  
@@ -41,4 +44,3 @@  
$(RUBY\_LIBS)   
$(RUBY\_LIBRUBYARG)

- 

# Index: rice/Makefile.am

— rice/Makefile.am (revision 173)  
+++ rice/Makefile.am (working copy)  
@@ -110,3 +110,5 @@

AM\_CPPFLAGS = @RUBY\_CPPFLAGS@

# +AM\_CXXFLAGS = @RUBY\_CXXFLAGS@ + Index: ruby.ac

— ruby.ac (revision 173)  
+++ ruby.ac (working copy)  
@@ -23,6 +23,7 @@  
RUBY\_CONFIG\_ARCHDIR=`RUBY_CONFIG(archdir)`  
RUBY\_CONFIG\_LIBDIR=`RUBY_CONFIG(libdir)`  
RUBY\_CONFIG\_BINDIR=`RUBY_CONFIG(bindir)`  
+RUBY\_CONFIG\_CFLAGS=`RUBY_CONFIG(CFLAGS)`  
RUBY\_CONFIG\_LIBS=`RUBY_CONFIG(LIBS)`  
RUBY\_CONFIG\_DLDLIBS=`RUBY_CONFIG(DLDLIBS)`  
RUBY\_CONFIG\_LDFLAGS=`RUBY_CONFIG(LDFLAGS)`  
@@ -32,6 +33,12 @@  
RUBY\_CPPFLAGS="-I${RUBY\_CONFIG\_ARCHDIR}"  
AC\_SUBST(RUBY\_CPPFLAGS)

+RUBY\_CFLAGS="${RUBY\_CONFIG\_CFLAGS}"  
+AC\_SUBST(RUBY\_CFLAGS)  
+  
+RUBY\_CXXFLAGS="${RUBY\_CONFIG\_CFLAGS}"  
+AC\_SUBST(RUBY\_CXXFLAGS)  
+  
RUBY\_LDFLAGS="-L${RUBY\_ARCHDIR} -L${RUBY\_CONFIG\_LIBDIR}  
${RUBY\_LDFLAGS}"  
AC\_SUBST(RUBY\_LDFLAGS)

Rice and the samples now build fine, though two unit tests still fail:

To\_From\_Ruby:int\_from\_ruby: integer -4611686018427387904 too small to  
convert to `int' To_From_Ruby:unsigned_int_from_ruby: integer 13835058055282163712 too big to convert to`unsigned int’

We will be sure to test x86\_64 better for the next release.

Paul
