CSC/ECE 517 Fall 2013/ch1 1w04 y: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 61: Line 61:
Next we should compile the files we have, use the following command:
Next we should compile the files we have, use the following command:


unix > swig -ruby example.i
$ swig -ruby example.i
unix > gcc -c -fpic example.c example_wrap.c -I/usr/local/include
 
unix > gcc -shared example.o example_wrap.o -o example.so
$ gcc -c -fpic example.c example_wrap.c -I/usr/local/include
 
$ gcc -shared example.o example_wrap.o -o example.so


The swig command produced a new file called example_wrap.c that should be compiled along with the example.c file. Most operating systems and scripting languages now support dynamic loading of modules.
The swig command produced a new file called example_wrap.c that should be compiled along with the example.c file. Most operating systems and scripting languages now support dynamic loading of modules.

Revision as of 14:33, 17 September 2013

Use SWIG to improve the performance of Ruby code

Introduction

What is SWIG

SWIG (Simplified Wrapper and Interface Generator) is a tool that allows programmers to mix C/C++ programs and libraries with several dynamic languages, including Tcl, Perl, Python, Ruby,and PHP. It was released in 1995, and the latest version is 2.0.10 which means it is still maintained and used widely.

Why we use SWIG

We all know that computer programming languages can be compiled or interpreted to execute instructions, and these languages can be divided into two basic groups: dynamic and static language.

As far as Ruby is concerned, it is a kind of dynamic language, which is high-level and useful for nearly everything as well as more flexible. On the other hand, Ruby also has many disadvantages as a script language. While using c/c++ code can do a support in many tasks to improve the memory usage and execution speed, we can not help thinking of combine these two kinds of languages and make the best use of them. That’s SWIG’s function, working by taking the declarations found in C/C++ header files and using them to generate the wrapper code that scripting languages need to access the underlying C/C++ code. In addition, SWIG provides a variety of customization features that let you tailor the wrapping process to suit your application. It is designed to work with existing c/c++ code, and turn common C/C++ libraries into components for use in popular scripting languages.

The primary purpose of using SWIG is to simplify the task of integrating c/c++ code with other programing languages, allowing people to focus on the underlying C program and using the high-level language interface, but not the tedious and complex chore of making the two languages talk to each other.

Besides, we can easily write test scripts to our code.

With the help and advantage of SWIG, we can make the program more robust and efficient.

Installation

Installation can be found here, and you may need the software of SWIG and the PCRE on your own computer to complete your installation.

A SWIG example

We will take a look at how we can use SWIG with respect to Ruby by using an example to illustrate. We will begin from creating a .c file.

The C code

Here we create a C code file named example.c :

/* File : example.c */

/* Compute the least common multiple of positive integers */

int lcm(int x, int y) {

 int g;
 g = y;
 while (x > 0) {
   g = x;
   x = y % x;
   y = g;
 }
 return x * y / g;

}

The SWIG interface

Then we create the SWIG interface file example.i:

/* File : example.i */ %module example

%inline %{ extern int lcm(int x, int y); %}

Compile the file

Next we should compile the files we have, use the following command:

$ swig -ruby example.i

$ gcc -c -fpic example.c example_wrap.c -I/usr/local/include

$ gcc -shared example.o example_wrap.o -o example.so

The swig command produced a new file called example_wrap.c that should be compiled along with the example.c file. Most operating systems and scripting languages now support dynamic loading of modules.

Build the Ruby module

Since we have example.so, the shared object file here, we can next use ruby to access the function and variables declared in the SWIG interface file (You can also use the command line to generate .bundle file to work as the library file of Ruby.).

unix > irb unix > require ‘example’ unix > true unix > Example.lcm(10,45) unix > 90

In our example, our Ruby module has been compiled into a shared library that can be loaded into Ruby.

Also, SWIG Tutorial gives us more examples using other languages, like python,Tcl.

Conclusion

SWIG is a great tool in an amount of sceneries. It can be used to provide a scripting interface to C/C++ code, making it easier for users and add extensions to your Ruby code or replacing existing modules with high-performance alternatives.

References

[1] The introduction to SWIG

[2] SWIG Tutorial

[3] What is Ruby and its feature

[4] SWIG Ruby examples