# Metakit from ruby using swig

**URL:** <https://www.ruby-forum.com/t/metakit-from-ruby-using-swig/68275>\
**Category:** Ruby\
**Created:** [September 11, 2006, 11:25am UTC](https://www.ruby-forum.com/t/metakit-from-ruby-using-swig/68275 "2006-09-11T11:25:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [September 11, 2006, 11:25am UTC](https://www.ruby-forum.com/t/metakit-from-ruby-using-swig/68275/1 "2006-09-11T11:25:07Z")

</div>

i managet to make the metakit ([Metakit embedded database library](http://www.equi4.com/metakit.html)) work  
from ruby  
by using swig. it was my first usage of swig and the result is not  
“rubyish”, but the basic test works (can store and retrieve data).

is there any interest in this (i.e. would you like the instructions on  
how i have done it)?.

vlad

---

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [September 11, 2006, 2:42pm UTC](https://www.ruby-forum.com/t/metakit-from-ruby-using-swig/68275/2 "2006-09-11T14:42:11Z")

</div>

Vladimir K. wrote:

> i managet to make the metakit ([Metakit embedded database library](http://www.equi4.com/metakit.html)) work  
> from ruby  
> by using swig. it was my first usage of swig and the result is not  
> “rubyish”, but the basic test works (can store and retrieve data).
> 
> is there any interest in this (i.e. would you like the instructions on  
> how i have done it)?.
> 
> vlad

I’m interested, although I’m perfectly happy with SQLite and its Ruby  
bindings. I’m just starting to get my feet wet with SWIG, so I’m  
interested in that as well.

---

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [September 11, 2006, 3:07pm UTC](https://www.ruby-forum.com/t/metakit-from-ruby-using-swig/68275/3 "2006-09-11T15:07:15Z")

</div>

here it goes…

/\* swig file \*/

%module mk4

%{  
#include “mk4.h”  
%}

%include “mk4.h”

/\* swig file end \*/

metakit version (the latest .tar.gz from their web-site).

swig version:  
SWIG Version 1.3.27  
Copyright © 1995-1998  
University of Utah and the Regents of the University of California  
Copyright © 1998-2005  
University of Chicago  
Compiled with g++ [i686-pc-linux-gnu]

ruby version:  
ruby 1.8.4 (2005-12-24) [i686-linux]

gcc version:  
g++ (GCC) 4.1.1

how to wrap the metakit using swig:

1. build and install metakit (as shared object, i did not try the static  
build)

2. cd to “include” directory in the metakit directory

3. create mk4.i

4. 
# swig -ruby -c++ mk4.i

(it will issue a lot of warnings but it generates the wrapper)

5. 
# g++ -c mk4\_wrap.cxx -I/usr/lib/ruby/1.8/i686-linux

it will give errors but deleting the 4 offending wrapper definitions  
(and  
4 calls using them) will make it compile

(the -I should point to wherewer the ruby.h resides)

6. 
# g++ -shared mk4\_wrap.o …/builds/libmk4.so -o mk4.so

note, the mk4so will depend on the location of libmk4.so, to build the  
“proper” way, copy the mk4\_wrap.o and libmk4.so to a common directory,  
cd there and do:

# g++ -shared mk4\_wrap.o libmk4.so -o mk4.so

(this way, if ld can find the libmk4.so, the mk4.so will load)

1. from ruby:  
require ‘mk4’  
include Mk4 # optional

(the mk4.so has to be somewhere ruby can find it)

notes: i am not sure whether the removal of definitions affects the  
function of metakit extension but a basic test works.

vlad

ps: if you are interested, i can send you the generated wrapper (so you  
can diff it against yours to see what i removed) + simple test script by  
e-mail…

---

<div class="post-metadata">

**Author:** ![prhlava](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71e660/32.png) [@prhlava](https://www.ruby-forum.com/u/prhlava)\
**Post date:** [September 11, 2006, 3:29pm UTC](https://www.ruby-forum.com/t/metakit-from-ruby-using-swig/68275/4 "2006-09-11T15:29:44Z")

</div>

forgot one thing, the metakit can be built as static library  
(–disable-shared option for configure), and the swig wrapper library  
linked against that - this way the ruby shared object (mk4.so) will not  
depend on the shared library libmk4.so (basically the ruby mk4.so will  
have all the functions of metakit in it)…

vlad
