# C++ pointers to Ruby objects

**URL:** <https://www.ruby-forum.com/t/c-pointers-to-ruby-objects/180334>\
**Category:** Ruby\
**Created:** [December 17, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/c-pointers-to-ruby-objects/180334 "2009-12-17T16:42:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![denommus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e99b99/32.png) [@denommus](https://www.ruby-forum.com/u/denommus)\
**Post date:** [December 17, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/c-pointers-to-ruby-objects/180334/1 "2009-12-17T16:42:40Z")

</div>

I’m trying to make an interface between PhysX and Ruby and I got stucked  
on the very beggining. XD

I have an NxPhysicsSDK pointer, pointing to an object (duh) and I would  
like to store this pointer into an Ruby VALUE object. I don’t really  
need to see it’s value outside the extension class (being clear:  
everything would be encapsulated), but I only find conversions from C  
pointers, not C++ objects.

Does anyone have a tip?

---

<div class="post-metadata">

**Author:** ![denommus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e99b99/32.png) [@denommus](https://www.ruby-forum.com/u/denommus)\
**Post date:** [December 17, 2009, 5:42pm UTC](https://www.ruby-forum.com/t/c-pointers-to-ruby-objects/180334/2 "2009-12-17T17:42:14Z")

</div>

You need to either build your own C wrapper around the C++ and expose  
that  
in Ruby as you cannot just map C++ objects to Ruby’s C-API.

Or you use a tool like Rice ( [http://rice.rubyforge.org](http://rice.rubyforge.org) ) or SWIG to  
wrap  
the C++ into Ruby.

I would also recommend looking into Rb++ (  
[http://rbplusplus.rubyforge.org](http://rbplusplus.rubyforge.org) )  
which IMO (though slightly biased) is easier to deal with than SWIG.

Jason

On Thu, Dec 17, 2009 at 10:42 AM, Yuri Albuquerque

---

<div class="post-metadata">

**Author:** ![denommus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e99b99/32.png) [@denommus](https://www.ruby-forum.com/u/denommus)\
**Post date:** [December 17, 2009, 6:09pm UTC](https://www.ruby-forum.com/t/c-pointers-to-ruby-objects/180334/3 "2009-12-17T18:09:23Z")

</div>

Well, I installed Rice’s gem and when I try to run my extconf.rb it  
returns:

extconf.rb:1:in `require’: no such file to load – mkmf-rice (LoadError)  
from extconf.rb:1

For some reason, Ruby is not finding this file.

I’ll try with Rb++ and see what I get.

---

<div class="post-metadata">

**Author:** ![denommus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e99b99/32.png) [@denommus](https://www.ruby-forum.com/u/denommus)\
**Post date:** [December 17, 2009, 6:19pm UTC](https://www.ruby-forum.com/t/c-pointers-to-ruby-objects/180334/4 "2009-12-17T18:19:11Z")

</div>

Yuri Albuquerque wrote:

> Well, I installed Rice’s gem and when I try to run my extconf.rb it  
> returns:
> 
> extconf.rb:1:in `require’: no such file to load – mkmf-rice (LoadError)  
> from extconf.rb:1
> 
> For some reason, Ruby is not finding this file.
> 
> I’ll try with Rb++ and see what I get.

Forget it. I forgot to include require ‘rubygems’ on my extconf.rb  
Thank you.  
But I’ll still try Rb++, too. ^^

---

<div class="post-metadata">

**Author:** ![denommus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e99b99/32.png) [@denommus](https://www.ruby-forum.com/u/denommus)\
**Post date:** [December 17, 2009, 8:41pm UTC](https://www.ruby-forum.com/t/c-pointers-to-ruby-objects/180334/5 "2009-12-17T20:41:16Z")

</div>

Well, I have two new doubts:

1 - When an object is created on PhysX, it’s not directly with a  
constructor. For example, if I want to create an NxPhysicsSDK object, I  
have to do this:  
NxPhysicsSDK \*sdk = NxCreatePhysicsSDK(NX\_PHYSICS\_SDK\_VERSION, 0, 0);  
What do I have to do to convert this on an initialize method on Rice?

2 - If I try to use Rb++, it asks for a namespace. PhysX does not use  
namespaces. What should I do in this case? Ignore this step?

---

<div class="post-metadata">

**Author:** ![denommus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e99b99/32.png) [@denommus](https://www.ruby-forum.com/u/denommus)\
**Post date:** [December 18, 2009, 10:45pm UTC](https://www.ruby-forum.com/t/c-pointers-to-ruby-objects/180334/6 "2009-12-18T22:45:15Z")

</div>

Yuri Albuquerque wrote:

> I’m trying to make an interface between PhysX and Ruby and I got stucked  
> on the very beggining. XD
> 
> I have an NxPhysicsSDK pointer, pointing to an object (duh) and I would  
> like to store this pointer into an Ruby VALUE object. I don’t really  
> need to see it’s value outside the extension class (being clear:  
> everything would be encapsulated), but I only find conversions from C  
> pointers, not C++ objects.

theoretically you should be able to store a struct in a ruby object that  
can contain pointers to anything.

> **[GitHub - rdp/google\_hash: wrapper for google's hash functions, for ruby](https://github.com/rdp/google_hash)**
>
> wrapper for google's hash functions, for ruby. Contribute to rdp/google\_hash development by creating an account on GitHub.

is an example project that has pointers to a C++ object [a google hash  
map].

The trick is wrapping all your ruby code in an extern “C” { } block.

GL.  
-r

---

<div class="post-metadata">

**Author:** ![denommus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e99b99/32.png) [@denommus](https://www.ruby-forum.com/u/denommus)\
**Post date:** [December 20, 2009, 10:04pm UTC](https://www.ruby-forum.com/t/c-pointers-to-ruby-objects/180334/7 "2009-12-20T22:04:21Z")

</div>

Roger P. wrote:

> theoretically you should be able to store a struct in a ruby object that  
> can contain pointers to anything.
> 
> [GitHub - rdp/google\_hash: wrapper for google's hash functions, for ruby](http://github.com/rdp/google_hash)
> 
> is an example project that has pointers to a C++ object [a google hash  
> map].
> 
> The trick is wrapping all your ruby code in an extern “C” { } block.
> 
> GL.  
> -r

Thanks to your tip, I am very closer to what I want to do than ever. I  
didn’t needed to make any gluecode between C and C++, I’m writing  
directly gluecode between Ruby and C++.

Thank you very much.
