# Installing C + Ruby extensions

**URL:** <https://www.ruby-forum.com/t/installing-c-ruby-extensions/131952>\
**Category:** Ruby\
**Created:** [March 14, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/installing-c-ruby-extensions/131952 "2008-03-14T18:14:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c37758/32.png) [@Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/u/Gerardo_SSGSSS_mez)\
**Post date:** [March 14, 2008, 6:14pm UTC](https://www.ruby-forum.com/t/installing-c-ruby-extensions/131952/1 "2008-03-14T18:14:45Z")

</div>

I’m using mkmf for building and installing a C extension. I’m also  
providing a gem.

I’m thinking of reorganizing the code to extract those C bits that can  
be coded in Ruby, and extend them in Ruby, while preserving a C core  
that provides a Ruby interface for the libraries I’m linking to.

My problem is how should my source tree look and what tools and how  
should I use to have both the Ruby and the C portion built and  
installed in the appropriate places.

Preferrably avoiding rubygems, so that I don’t force my users to have  
rubygems.

Which makes me ask: if rubygems will be included in Ruby 2.0, ¿will it  
be mandatory?, ¿would it make sense not to make a gem for an  
extension?

---

<div class="post-metadata">

**Author:** ![Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c37758/32.png) [@Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/u/Gerardo_SSGSSS_mez)\
**Post date:** [March 14, 2008, 7:03pm UTC](https://www.ruby-forum.com/t/installing-c-ruby-extensions/131952/2 "2008-03-14T19:03:25Z")

</div>

Gerardo S. Gómez Garrido wrote:

> Preferrably avoiding rubygems, so that I don’t force my users to have rubygems.
> 
> Which makes me ask: if rubygems will be included in Ruby 2.0, ¿will it  
> be mandatory?, ¿would it make sense not to make a gem for an  
> extension?
> 
> –  
> Gerardo S.

I use Minero A.'s setup.rb (you can find it on the RAA) for RMagick.  
The code is a bit opaque but it’s robust and mature. The source/build  
tree that setup uses is here:  
[Creating Packages with setup.rb](http://i.loveruby.net/en/projects/setup/doc/devel.html). RubyGems works  
well with setup.rb, and it’s not hard to build a tar.gz version as well  
as a gem.

I do not recommend avoiding RubyGems. Unless the typical consumer of  
your extension is different from the mainstream of Ruby programmers, I  
don’t think it’s going to be a problem “forcing” them to have RubyGems.  
In my experience 99% of Rubyists use RubyGems and probably half of them  
are not able to install an extension that is not supplied in the form of  
a gem.

RubyGems is built in to 1.9.0. While I don’t think its use will be  
_mandantory_ it seems very useful to supply your extension as a gem.  
Since the main problem with RubyGems today is that people don’t know to  
require “rubygems” before requiring the gem, having RubyGems built in to  
Ruby will be very nice indeed, both for us and for our users.

---

<div class="post-metadata">

**Author:** ![Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c37758/32.png) [@Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/u/Gerardo_SSGSSS_mez)\
**Post date:** [March 15, 2008, 3:18am UTC](https://www.ruby-forum.com/t/installing-c-ruby-extensions/131952/3 "2008-03-15T03:18:02Z")

</div>

Thanks Tim for your reply, it has been very helpful.

I have another question.

I have a C core. I’m already extending it in Ruby. There’s C code that  
could be moved to Ruby. Would I gain anything doing this?

---

<div class="post-metadata">

**Author:** ![Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c37758/32.png) [@Gerardo\_SSGSSS\_mez](https://www.ruby-forum.com/u/Gerardo_SSGSSS_mez)\
**Post date:** [March 15, 2008, 3:37am UTC](https://www.ruby-forum.com/t/installing-c-ruby-extensions/131952/4 "2008-03-15T03:37:24Z")

</div>

Gerardo S. Gómez Garrido wrote:

> Thanks Tim for your reply, it has been very helpful.
> 
> I have another question.
> 
> I have a C core. I’m already extending it in Ruby. There’s C code that  
> could be moved to Ruby. Would I gain anything doing this?

I think so. Ruby code is portable (that is, you know it will run because  
Ruby is already running) and easier to maintain than C code.

RMagick started life as 100% C but after almost a year of work I threw  
all the code away. I started over using as much Ruby as possible and  
never looked back.
