C and c++++ are recognized as the preferred platform for creating high-performance code. A common requirement for http://www.aliyun.com/zixun/aggregation/7155.html "> Developers is to expose C + + code to the scripting language interface, which is exactly what simplified wrappers and Interface Generator (SWIG). SWIG allows you to expose C + + code to a wide range of scripting languages, including Ruby, Perl, TCL, and Python. This article uses Ruby as the preferred scripting interface for exposing the C + + functionality. To understand this article, you must have the appropriate knowledge of C + + and Ruby.
SWIG is a good tool to suit a variety of scenarios, including:
Provide a script interface to C/C + + code make it easier for users to add extensions to your Ruby code or replace existing ones with high-performance alternative modules to provide the ability to use a scripting environment for code execution units and integration testing use TK to develop a graphical user interface and integrate it with C + + back-end The GNU Debugger needs to be triggered every time, and SWIG is easier to debug than it is.
SWIG Installation
This article uses the SWIG version 2.0.4 (see Resources for a link to the download site). To build and install SWIG, follow the typical open source installation process by entering the following command at a command prompt:
Tar xvzf swig-2.0.4.tar.gz
./configure–prefix=/your/swig/install/path
Make
Make install
Note that the path provided for the prefix must be an absolute path.
Ruby Environment variables
SWIG needs to be ruby.h to ensure proper compilation when generating wrapper-C + + code. Check Ruby.h in your Ruby installation: One suggestion is to point the environment variable ruby_include to the folder that contains the Ruby.h and point ruby_lib to the path that contains the Ruby library.
Use SWIG to write Hello world
As input, SWIG requires a file that contains ANSI/C + + declarations and SWIG directives. I refer to this input file as the SWIG interface file. Be sure to remember that SWIG only needs enough information to generate wrapper code. The interface file usually has a *.i or *.SWG extension. The following is the first extended file test.i:
%module test%constant Char Text = "Hello World with SWIG"
Run this code using SWIG:
Swig–ruby test.i
The command line in the second code snippet generates a file named Test_wrap.c in the current folder. Now you need to create a shared library in this C file. The following is the command line:
bash$ gcc–fpic–c test_wrap.c–i$ruby_includebash$ gcc–shared test_wrap.o–o test_wrap.so–lruby–l$ruby_lib
It's as simple as that. You are ready to trigger the interactive Ruby Shell (IRB) and enter require ' test_wrap ' to check the Ruby test module and its contents. Here is the extended Ruby end:
IRB (main):001:0> require ' Test_wrap ' => trueirb (main):002:0> test.constants=> ["Text"]IRB (main): 003:0 > Test:: text=> "Hello World with SWIG"
SWIG can be used to generate various language extensions, just run swig–help check all available options. For Ruby, you can enter Swig–ruby <interface file>; for Perl, you can use Swig–perl <interface file>.
You can also use SWIG to generate C + + code: Just use –c++ at the command line. In the previous example, running Swig–c++–ruby test.i generates a file named Test_wrap.cxx in the current folder.