# Name clash in C extension

**URL:** <https://www.ruby-forum.com/t/name-clash-in-c-extension/107684>\
**Category:** Ruby\
**Created:** [June 25, 2007, 10:16am UTC](https://www.ruby-forum.com/t/name-clash-in-c-extension/107684 "2007-06-25T10:16:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oscar-alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e36b37/32.png) [@oscar-alexander](https://www.ruby-forum.com/u/oscar-alexander)\
**Post date:** [June 25, 2007, 10:16am UTC](https://www.ruby-forum.com/t/name-clash-in-c-extension/107684/1 "2007-06-25T10:16:47Z")

</div>

Hi,

I’m trying to write an extension interfacing with the C iGraph  
library ([http://cneurocvs.rmki.kfki.hu/igraph/](http://cneurocvs.rmki.kfki.hu/igraph/)). Unfortunately the  
iGraph header files define a macro called TYPE which clashes with the  
TYPE macro defined in ruby.h. Both headers are obviously required to  
compile my extension.

What is the best practice for dealing with name clashes like this? My  
current solution is to do a search and replace in the iGraph source  
changing TYPE to IGRAPH\_TYPE and recompile the library. That works  
for now, but implies anyone else wanting to build my extension will  
have to manually hack iGraph themselves which seems a bit off. Am I  
missing some obvious solution other than emailing iGraph/Ruby core  
and asking for them to play a bit nicer with their names?

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![oscar-alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e36b37/32.png) [@oscar-alexander](https://www.ruby-forum.com/u/oscar-alexander)\
**Post date:** [June 25, 2007, 1:05pm UTC](https://www.ruby-forum.com/t/name-clash-in-c-extension/107684/2 "2007-06-25T13:05:27Z")

</div>

On 6/25/07, Alex G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I’m trying to write an extension interfacing with the C iGraph  
> library ([http://cneurocvs.rmki.kfki.hu/igraph/](http://cneurocvs.rmki.kfki.hu/igraph/)). Unfortunately the  
> iGraph header files define a macro called TYPE which clashes with the  
> TYPE macro defined in ruby.h. Both headers are obviously required to  
> compile my extension.

What happens if you define a wrapper for the calls you make to the  
IGraph  
liibrary? That way you include only the wrapper’s header in the  
extension’s  
main source file (the one that includes ruby.h and defines Init\_xxx).  
You’ll  
have to massage all data items between Ruby-land and C in the wrapper as  
well. I took this approach in the EventMachine library (where it has an  
additional benefit because the wrapper is in C, but the extension code  
is  
all in C++, so the wrapper approach avoids exposing decorated symbols in  
the  
compiled extension).

---

<div class="post-metadata">

**Author:** ![oscar-alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e36b37/32.png) [@oscar-alexander](https://www.ruby-forum.com/u/oscar-alexander)\
**Post date:** [June 26, 2007, 2:09am UTC](https://www.ruby-forum.com/t/name-clash-in-c-extension/107684/3 "2007-06-26T02:09:04Z")

</div>

On 25 Jun 2007, at 20:04, Francis C. wrote:

> What happens if you define a wrapper for the calls you make to the  
> code is  
> all in C++, so the wrapper approach avoids exposing decorated  
> symbols in the  
> compiled extension).

Thanks for the pointer. I’d been trying to shortcut and use SWIG to  
generate the extension, but it looks like I will have to abandon that  
for the time being anyway (too complicated, makes my head hurt). I  
will have a look at EventMachine and see if I can use the same  
wrapper technique here.

Alex G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![oscar-alexander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/e36b37/32.png) [@oscar-alexander](https://www.ruby-forum.com/u/oscar-alexander)\
**Post date:** [July 3, 2007, 4:07pm UTC](https://www.ruby-forum.com/t/name-clash-in-c-extension/107684/4 "2007-07-03T16:07:47Z")

</div>

Alex, i’m an igraph developer, and there is an easy solution for this i  
think,  
all you need is to append

#ifdef TYPE  
#undef TYPE  
#endif

#ifdef FUNCTION  
#undef FUNCTION  
#endif

to the file igraph\_pmt\_off.h. (This was an igraph bug, not having these  
there.) Please feel free to contact me if you need any help.

Best,  
Gabor

Alex G. wrote:

> Hi,
> 
> I’m trying to write an extension interfacing with the C iGraph  
> library ([http://cneurocvs.rmki.kfki.hu/igraph/](http://cneurocvs.rmki.kfki.hu/igraph/)). Unfortunately the  
> iGraph header files define a macro called TYPE which clashes with the  
> TYPE macro defined in ruby.h. Both headers are obviously required to  
> compile my extension.
> 
> What is the best practice for dealing with name clashes like this? My  
> current solution is to do a search and replace in the iGraph source  
> changing TYPE to IGRAPH\_TYPE and recompile the library. That works  
> for now, but implies anyone else wanting to build my extension will  
> have to manually hack iGraph themselves which seems a bit off. Am I  
> missing some obvious solution other than emailing iGraph/Ruby core  
> and asking for them to play a bit nicer with their names?
> 
> Alex G.
> 
> Bioinformatics Center  
> Kyoto University
