# Ruby/C++ integration with SWIG: problem with namespaces

**URL:** <https://www.ruby-forum.com/t/ruby-c-integration-with-swig-problem-with-namespaces/132366>\
**Category:** Ruby\
**Created:** [March 19, 2008, 1:54pm UTC](https://www.ruby-forum.com/t/ruby-c-integration-with-swig-problem-with-namespaces/132366 "2008-03-19T13:54:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![comfortably\_numb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@comfortably\_numb](https://www.ruby-forum.com/u/comfortably_numb)\
**Post date:** [March 19, 2008, 1:54pm UTC](https://www.ruby-forum.com/t/ruby-c-integration-with-swig-problem-with-namespaces/132366/1 "2008-03-19T13:54:05Z")

</div>

Hi everyone,

I’m trying to import a C++ library in Ruby using SWIG, but I have a  
problem with namespaces and enumerators: I have a namespace “mymodule”  
with a function “myfunction” which takes an element of type “myenum” as  
an argument.

If I define “myfunction” and “myenum” in the same source file  
“mymodule.h” everything works, but if I separate them, placing “myenum”  
in “myenum.h” something odd happens: I can print the value of an element  
of “myenum” via Ruby but cannot pass it as an argument to “myfunction”.

Here are the source files and the error I get:

##################

### mymodule.h

##################

#ifndef MYMODULE\_H  
#define MYMODULE\_H

#include “myenum.h”

namespace mymodule {

void myfunction(myenum);

}

#endif

################

### myenum.h

################

#ifndef MYENUM\_H  
#define MYENUM\_H

namespace mymodule {

enum myenum { A, B, C };

}

#endif

####################

### mymodule.cxx

####################

#include “mymodule.h”

namespace mymodule {

void myfunction(myenum c)  
{  
// do nothing  
}

}

##################

### mymodule.i

##################

%module mymodule

%{  
#include “mymodule.h”  
#include “myenum.h”

using namespace mymodule;  
%}

%include mymodule.h  
%include myenum.h

################

### myapp.rb

################

require ‘mymodule’

print "I can print Mymodule::B = ", Mymodule::B, “\n”

print “but I cannot pass it as an argument:\n”

Mymodule::myfunction(Mymodule::B)

####################

### command-line

####################

$ swig -c++ -ruby mymodule.i

$ g++ -fPIC -c mymodule\_wrap.cxx -I/usr/lib/ruby/1.8/x86\_64-linux

$ g++ -fPIC -c mymodule.cxx

$ g++ -shared -o mymodule.so \*.o

$ ruby myapp.rb

I can print Mymodule::B = 1

but I cannot pass it as an argument:

myapp.rb:7:in `myfunction’: Expected argument 0 of type myenum, but got  
Fixnum 1 (TypeError)  
in SWIG method ‘mymodule::myfunction’ from myapp.rb:7

---

<div class="post-metadata">

**Author:** ![comfortably\_numb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@comfortably\_numb](https://www.ruby-forum.com/u/comfortably_numb)\
**Post date:** [March 19, 2008, 8:26pm UTC](https://www.ruby-forum.com/t/ruby-c-integration-with-swig-problem-with-namespaces/132366/2 "2008-03-19T20:26:18Z")

</div>

SOLVED

It turned out I need to write mymodule::myenum in my C++ code even if I  
used the “using namespace” directive, since Swig doesn’t understand it.

This page helped me to figure this out:

> **[Yahoo | Mail, Weather, Search, Politics, News, Finance, Sports & Videos](https://www.yahoo.com/)**
>
> Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!
