Testing C++ in Ruby, continued

For a while I’ve been working with SWIG to generate wrappers for my C++ code. I ran into some problems when using it in a real project, so I’ve made the following adjustments to my method. First of all I’m using mkmf to generate a Makefile for the shared objects. This is how the SWIG documentation shows it, and I have added some lines to link in more libraries and run the swig command. I’ve added the -minherit flag to support C++ inheritance.

 require 'mkmf'
# Create wrapper module
`swig -c++ -ruby -minherit -Wall -o units_wrap.cpp units.i`

# Since the SWIG runtime support library for Ruby
# depends on the Ruby library, make sure it's in the list
# of libraries.
$libs = append_library($libs, Config::CONFIG['RUBY_INSTALL_NAME'])

# Also, we need the c++ libraries
$libs = append_library($libs, "stdc++")

# Create the makefile
create_makefile('units') 

This script, src/extconf.rb is executed by automake by adding the following in src/Makefile.am:

bin_PROGRAMS = example
example_SOURCES = example.cpp
noinst_unit_testsdir = .
noinst_unit_tests_DATA = units.so
EXTRA_DIST = extconf.rb autogen.sh
units.so:
     ruby extconf.rb
     make

I love this solution because now I only have 2 files to maintain compiler details instead of 3. src/units.i and src/Makefile.am. Also, by using the noinst_ prefix the module will not be included when I run ‘make dist’. You can see how it all fits together in my APR project.


Trackbacks

Use the following link to trackback from your own site:
http://blog.aczid.nl/trackbacks?article_id=14


Me elsewhere: