# Multiple c extensions with the same function names

**URL:** <https://www.ruby-forum.com/t/multiple-c-extensions-with-the-same-function-names/159337>\
**Category:** Ruby\
**Created:** [February 3, 2009, 4:34pm UTC](https://www.ruby-forum.com/t/multiple-c-extensions-with-the-same-function-names/159337 "2009-02-03T16:34:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![icottee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3be4f8/32.png) [@icottee](https://www.ruby-forum.com/u/icottee)\
**Post date:** [February 3, 2009, 4:34pm UTC](https://www.ruby-forum.com/t/multiple-c-extensions-with-the-same-function-names/159337/1 "2009-02-03T16:34:01Z")

</div>

Hi,

A bit of a noob questions here, so please bear with me. I’ve been  
attempting to use multiple C extensions containing identical c function  
names. I had assumed that because the functions were in different  
modules (.so dynamic libraries) it would be unlikely for there to be any  
name clashes. However, when creating new instances from these loaded  
modules, there seems to be an issue with instances calling functions  
belonging to other classes.

For example, assuming the source file TestA.c contains:

#include “ruby.h”  
#include “malloc.h”

VALUE CTestA;

typedef struct \_tba {  
int statusf;  
int request;  
} TestAbox;

static void c\_free(void \*p) {  
TestAbox _data = (TestAbox_)p;  
free(data);  
}

static VALUE  
c\_test(VALUE self)  
{  
TestAbox \*ptr;  
Data\_Get\_Struct(self, TestAbox, ptr);

printf(“TestA test function called\n”);  
return Qnil;  
}

static VALUE  
c\_init(VALUE self, VALUE unit)  
{  
printf(“TestA new init\n”);  
return self;  
}

VALUE c\_new(VALUE class)  
{  
printf(“Creating new TestA instance\n”);  
TestAbox _ptr = (TestAbox_)malloc(sizeof(TestAbox));  
VALUE tdata = Data\_Wrap\_Struct(class, 0, c\_free, ptr);  
rb\_obj\_call\_init(tdata, 0, NULL);  
printf(“Successfully created TestA instance\n”);  
return tdata;  
}

With another TestB.c containing the same source but with any occurrence  
of “TestA” having been replaced with “TestB”

Compiled against ruby 1.8.4 (2005-12-24) [x86\_64-linux], using gcc  
-shared to produce two .so files, TestA.so and TestB.so.

Then within irb:

irb(main):001:0\> require ‘testa/TestA’  
Initialising TestA  
TestA initialised  
=\> true  
irb(main):002:0\> require ‘testb/TestB’  
Initialising TestB  
TestB initialised  
=\> true  
irb(main):003:0\> testa=TestA.new()  
Creating new TestA instance  
TestA new init  
Successfully created TestA instance  
=\> #TestA:0x2ab3676822e0  
irb(main):004:0\> testb=TestB.new()  
Creating new TestA instance  
TestB new init  
Successfully created TestA instance  
=\> #TestB:0x2ab367676850  
irb(main):005:0\>

I would be very grateful if someone could explain what was happening  
here. My guess would be memory addresses being overwritten for symbols  
with the same name when modules are loaded in. It would just be nice to  
know that it’s not something more sinister that I am doing incorrectly.  
So if this is not how Ruby C extensions should be used, is the answer  
to use unique names between modules? This just seems a little odd to me.

Many thanks.

---

<div class="post-metadata">

**Author:** ![icottee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3be4f8/32.png) [@icottee](https://www.ruby-forum.com/u/icottee)\
**Post date:** [February 3, 2009, 4:59pm UTC](https://www.ruby-forum.com/t/multiple-c-extensions-with-the-same-function-names/159337/2 "2009-02-03T16:59:43Z")

</div>

Hi,

At Wed, 4 Feb 2009 00:33:40 +0900,  
Ian Campbell wrote in [ruby-talk:326749]:

> I would be very grateful if someone could explain what was happening  
> here. My guess would be memory addresses being overwritten for symbols  
> with the same name when modules are loaded in. It would just be nice to  
> know that it’s not something more sinister that I am doing incorrectly.

Ruby uses RTLD\_GLOBAL flag to dlopen() on Linux, but it can  
vary on other platforms. You can’t depend on whether a symbols  
in an extension library is visible from another extension  
library.

> So if this is not how Ruby C extensions should be used, is the answer  
> to use unique names between modules? This just seems a little odd to me.

Making them static except for Init function.

---

<div class="post-metadata">

**Author:** ![icottee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/3be4f8/32.png) [@icottee](https://www.ruby-forum.com/u/icottee)\
**Post date:** [February 4, 2009, 11:08am UTC](https://www.ruby-forum.com/t/multiple-c-extensions-with-the-same-function-names/159337/3 "2009-02-04T11:08:34Z")

</div>

Hi Nobuyoshi,

Many thanks for your explanation; the use of dlopen() with RTLD\_GLOBAL  
makes perfect sense.

Full steam ahead now!
