# Supporting both ruby 1.8 and 1.9 in a compiled library

**URL:** <https://rubytalk.org/t/supporting-both-ruby-1-8-and-1-9-in-a-compiled-library/63301>\
**Category:** ruby-talk\
**Created:** [11 August 2011 06:56 UTC](https://rubytalk.org/t/supporting-both-ruby-1-8-and-1-9-in-a-compiled-library/63301 "2011-08-11T06:56:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![James4](https://avatars.discourse-cdn.com/v4/letter/j/9f8e36/32.png) [@James4](https://rubytalk.org/u/James4)\
**Post date:** [11 August 2011 06:56 UTC](https://rubytalk.org/t/supporting-both-ruby-1-8-and-1-9-in-a-compiled-library/63301/1 "2011-08-11T06:56:50Z")

</div>

Hi all,

I am a maintainer of the Ruby interface to a C++ library wrapped using  
SWIG. Up until now I have only supported Ruby 1.8 but I'm now adding  
support for Ruby 1.9. Are there any recommendations for how to allow  
both compiled copies of this wrapped library to be on the load path  
($RUBYLIB) given that the \*.so files are different for 1.8 and 1.9?  
(Some of my users will use 1.8 and others will use 1.9 but $RUBYLIB is  
shared). I was thinking of my own solution similar to (module is "oa"):

oa.rb  
oa/1.8/x86\_64-linux/\*.so  
oa/1.9/x86\_64-linux/\*.so

And the oa.rb file will have custom code written to dispatch to one set  
of the \*.so files based on the Ruby version. Are there any better ways?  
Sorry if this is a basic question... I could not find the answer by  
doing some Internet searches.

I'd prefer to not use gems for this since I will not distribute it  
online and Ruby 1.8 is not guaranteed to have rubygems installed (trying  
to keep this as flexible and simple as possible).

Thanks in advance...

-James

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Hans\_Mackowiak](https://avatars.discourse-cdn.com/v4/letter/h/f08c70/32.png) [@Hans\_Mackowiak](https://rubytalk.org/u/Hans_Mackowiak)\
**Post date:** [11 August 2011 07:39 UTC](https://rubytalk.org/t/supporting-both-ruby-1-8-and-1-9-in-a-compiled-library/63301/2 "2011-08-11T07:39:04Z")

</div>

the better way: build your \*.so twice, one for each version and put the  
files in the vendor\_ruby path

so require "yourname" does work for both and you does not need an extra  
rb file

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Luis\_Lavena1](https://avatars.discourse-cdn.com/v4/letter/l/f08c70/32.png) [@Luis\_Lavena1](https://rubytalk.org/u/Luis_Lavena1)\
**Post date:** [11 August 2011 14:32 UTC](https://rubytalk.org/t/supporting-both-ruby-1-8-and-1-9-in-a-compiled-library/63301/3 "2011-08-11T14:32:24Z")

</div>

James Masters wrote in post #1016122:

> Hi all,
> 
> I am a maintainer of the Ruby interface to a C++ library wrapped using  
> SWIG. Up until now I have only supported Ruby 1.8 but I'm now adding  
> support for Ruby 1.9. Are there any recommendations for how to allow  
> both compiled copies of this wrapped library to be on the load path  
> ($RUBYLIB) given that the \*.so files are different for 1.8 and 1.9?  
> (Some of my users will use 1.8 and others will use 1.9 but $RUBYLIB is  
> shared). I was thinking of my own solution similar to (module is "oa"):
> 
> oa.rb  
> oa/1.8/x86\_64-linux/\*.so  
> oa/1.9/x86\_64-linux/\*.so

Perhaps you can take a look to rake-compiler:

> **[GitHub - rake-compiler/rake-compiler: Provide a standard and simplified way...](https://github.com/rake-compiler/rake-compiler)**
>
> Provide a standard and simplified way to build and package Ruby C and Java extensions using Rake as glue. - GitHub - rake-compiler/rake-compiler: Provide a standard and simplified way to build and ...

Which provides what is called "fat-binaries" and version out the  
directory of Ruby implementation using this technique.

The other approach is use the RbConfig::CONFIG["ruby\_version"] which  
will provide you the API compatibility version, so 1.8 for 1.8.6, 1.8.7  
and 1.9.1 for Ruby 1.9.1, 1.9.2 and 1.9.3

> And the oa.rb file will have custom code written to dispatch to one set  
> of the \*.so files based on the Ruby version. Are there any better ways?  
> Sorry if this is a basic question... I could not find the answer by  
> doing some Internet searches.
> 
> I'd prefer to not use gems for this since I will not distribute it  
> online and Ruby 1.8 is not guaranteed to have rubygems installed (trying  
> to keep this as flexible and simple as possible).

Then you should use "setup.rb" which will install the compiled extension  
in the platform specific folder, except for the versioned part.

Good luck,

> **···**
>
> --  
> Luis Lavena
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![James4](https://avatars.discourse-cdn.com/v4/letter/j/9f8e36/32.png) [@James4](https://rubytalk.org/u/James4)\
**Post date:** [11 August 2011 18:52 UTC](https://rubytalk.org/t/supporting-both-ruby-1-8-and-1-9-in-a-compiled-library/63301/4 "2011-08-11T18:52:27Z")

</div>

Thanks for the responses so far.

Hans - I didn't mention that I do not have privileges to modify the ruby  
installation... I'm a user in our system (not an admin) and need to  
store the libraries in a location other than the ruby install.

Luis - good suggestions but I'm more focused on how to set it up so I  
can access the libraries using the same $RUBYLIB (or an equivalent) for  
both 1.8 and 1.9 as opposed to the steps related to  
compiling/installing.

Thanks,  
-James

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Hans\_Mackowiak](https://avatars.discourse-cdn.com/v4/letter/h/f08c70/32.png) [@Hans\_Mackowiak](https://rubytalk.org/u/Hans_Mackowiak)\
**Post date:** [11 August 2011 18:57 UTC](https://rubytalk.org/t/supporting-both-ruby-1-8-and-1-9-in-a-compiled-library/63301/5 "2011-08-11T18:57:48Z")

</div>

thats why gem is better, it manage that the stuff is installed into the  
right file (even as user)  
its also possible with only mkmf but very bad

and PS: 1.8 should die

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Ralf\_Mueller1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ralf_mueller1/32/2123_2.png) [@Ralf\_Mueller1](https://rubytalk.org/u/Ralf_Mueller1)\
**Post date:** [11 August 2011 19:09 UTC](https://rubytalk.org/t/supporting-both-ruby-1-8-and-1-9-in-a-compiled-library/63301/6 "2011-08-11T19:09:52Z")

</div>

Hi James,  
I had a similar problem to solve in the CDI project: [https://code.zmaw.de/projects/cdi](https://code.zmaw.de/projects/cdi). You can register yourself and watch in the repository ([https://code.zmaw.de/projects/cdi/repository/show/trunk/cdi/interfaces](https://code.zmaw.de/projects/cdi/repository/show/trunk/cdi/interfaces)) or take a look into the tar ball [https://code.zmaw.de/attachments/download/1537/cdi-1.5.1.tar.gz](https://code.zmaw.de/attachments/download/1537/cdi-1.5.1.tar.gz).  
I did not use gem which might have been the better solution.

cheers  
ralf
