# Multiple C-Extensions with mkmf

**URL:** <https://www.ruby-forum.com/t/multiple-c-extensions-with-mkmf/149297>\
**Category:** Ruby\
**Created:** [September 23, 2008, 11:59am UTC](https://www.ruby-forum.com/t/multiple-c-extensions-with-mkmf/149297 "2008-09-23T11:59:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_B\_Gardner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@Matthew\_B\_Gardner](https://www.ruby-forum.com/u/Matthew_B_Gardner)\
**Post date:** [September 23, 2008, 11:59am UTC](https://www.ruby-forum.com/t/multiple-c-extensions-with-mkmf/149297/1 "2008-09-23T11:59:58Z")

</div>

Hello –  
I was wondering if there’s a way to separate multiple C-extension  
classes  
into separate files but still be able to directly call functions  
(C-style  
calls)? I currently have a bunch of classes in one file for the  
performance  
boost of direct calls over ruby function calls (not Ruby-ish, I know,  
but the  
boost was significant), but the sloppiness isn’t very appealing,  
especially  
as the file continues to grow. Hopefully there’s a solution that will  
allow  
me to separate the classes without hindering performance?

I appreciate any help,  
Matt

---

<div class="post-metadata">

**Author:** ![Matthew\_B\_Gardner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@Matthew\_B\_Gardner](https://www.ruby-forum.com/u/Matthew_B_Gardner)\
**Post date:** [September 23, 2008, 4:50pm UTC](https://www.ruby-forum.com/t/multiple-c-extensions-with-mkmf/149297/2 "2008-09-23T16:50:09Z")

</div>

Hi,

On Tue, Sep 23, 2008 at 06:59:06PM +0900, Matthew B Gardner wrote:

> Hello –  
> I was wondering if there’s a way to separate multiple C-extension classes  
> into separate files but still be able to directly call functions (C-style  
> calls)? I currently have a bunch of classes in one file for the performance  
> boost of direct calls over ruby function calls (not Ruby-ish, I know, but the  
> boost was significant), but the sloppiness isn’t very appealing, especially  
> as the file continues to grow. Hopefully there’s a solution that will allow  
> me to separate the classes without hindering performance?

I think we’ll probably need some clarification. Do you have 1  
extensions that you want to start separating out into multiple .c files?  
This is easily done. Just put them all in separate .c files and in the  
top level Init\_myextname() function call other initialization functions.

I do this in Amalgalite, and I started using this approach after I saw  
how the extensions were built for ‘rev’. Take a look at those two gems  
and see if how their extensions are built help you out.

enjoy,

-jeremy

---

<div class="post-metadata">

**Author:** ![Matthew\_B\_Gardner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@Matthew\_B\_Gardner](https://www.ruby-forum.com/u/Matthew_B_Gardner)\
**Post date:** [October 1, 2008, 5:43pm UTC](https://www.ruby-forum.com/t/multiple-c-extensions-with-mkmf/149297/3 "2008-10-01T17:43:28Z")

</div>

On Tue, Sep 23, 2008 at 06:59:06PM +0900, Matthew B Gardner wrote:

> Hello –  
> I was wondering if there’s a way to separate multiple C-extension classes  
> into separate files but still be able to directly call functions (C-style  
> calls)? I currently have a bunch of classes in one file for the performance  
> boost of direct calls over ruby function calls (not Ruby-ish, I know, but the  
> boost was significant), but the sloppiness isn’t very appealing, especially  
> as the file continues to grow. Hopefully there’s a solution that will allow  
> me to separate the classes without hindering performance?

Ruby loads extensions with RTLD\_GLOBAL, so you should be able to access  
exported symbols in another extension if you require that extension  
using rb\_require().

Paul

---

<div class="post-metadata">

**Author:** ![Matthew\_B\_Gardner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ac91a4/32.png) [@Matthew\_B\_Gardner](https://www.ruby-forum.com/u/Matthew_B_Gardner)\
**Post date:** [October 1, 2008, 8:43pm UTC](https://www.ruby-forum.com/t/multiple-c-extensions-with-mkmf/149297/4 "2008-10-01T20:43:50Z")

</div>

Hi,

At Thu, 2 Oct 2008 00:41:33 +0900,  
Paul B. wrote in [ruby-talk:316603]:

> Ruby loads extensions with RTLD\_GLOBAL, so you should be able to access  
> exported symbols in another extension if you require that extension  
> using rb\_require().

Don’t rely on it, since it’s not possible on all platforms.
