Implement functionwrapper

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-sploving@11242 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Baozeng Ding 2009-05-30 07:10:19 +00:00
commit 4aa72f9b01
16 changed files with 739 additions and 12 deletions

175
Doc/Manual/Scilab.html Normal file
View file

@ -0,0 +1,175 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>SWIG and Scilab</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body bgcolor="#ffffff">
<H1><a name="Scilab"></a>36 SWIG and Scilab</H1>
<!-- INDEX -->
<div class="sectiontoc">
<ul>
<li><a href="#Scilab_nn2">Preliminaries</a>
<li><a href="#Scilab_nn3">Running SWIG</a>
<ul>
<li><a href="#Scilab_nn5">Compiling a dynamic module</a>
<li><a href="#Scilab_nn6">Using your module</a>
</ul>
<li><a href="#Scilab_nn7">A tour of basic C/C++ wrapping</a>
<ul>
<li><a href="#Scilab_nn8">Modules</a>
<li><a href="#Scilab_nn9">Functions</a>
</ul>
</ul>
</div>
<!-- INDEX -->
<p>
Scilab is a scientific software package for numerical computations providing a powerful open computing environment for engineering and scientific applications that is mostly compatible with MATLAB.More information can be found at <a href="http://www.scilab.org">www.scilab.org</a>.
</p>
<p>
This chapter is intended to give an introduction to using the module. You should also read the SWIG documentation that is not specific to Scilab.Also, there are a dozen or so examples in the Examples/Scilab directory.
</p>
<H2><a name="Scilab_nn2"></a>36.1 Preliminaries</H2>
<p>
The current SWIG implemention is based on Scilab 5.1.1. Support for other higher versions has not been tested, nor has support for any OS other than Linux.
</p>
<H2><a name="Scilab_nn3"></a>36.2 Running SWIG</H2>
<p>
Let's start with a very simple SWIG interface file:
</p>
<div class="code"><pre>%module example
%{
#include "example.h"
%}
int gcd(int x, int y);
extern double Foo; </pre></div>
<p>
To build an Scilab module, run SWIG using the <tt>-scilab</tt> option.
</p>
<div class="shell"><pre>$ swig -scilab example.i </pre></div>
<p>
This creates a C source file <tt>example_wrap.c</tt>and a interface file <tt>builder.sce</tt>. The generated C source file contains the low-level wrappers that need to be compiled and linked with the rest of your C application (in this case, the gcd implementation) to create an extension module. And the builder.sce is used to generate the *.so file.
</p>
<p>
The swig command line has a number of options you can use, like to redirect it's output. Use <tt>swig --help</tt> to learn about these.
</p>
<H3><a name="Scilab_nn5"></a>36.2.1 Compiling a dynamic module</H3>
<p>
Scilab modules are shared objects having the ".so" suffix.
Building such a file is usually done with the "exec" command (within Scilab itself) For example,
</p>
<div class="shell"><pre>
$ ./scilab
--> exec builder.sce
</pre></div>
<p>
where builder.sce is the interface file generated by the swig. It looks like the following:
</p>
<div class="shell"><pre>
ilib_name = "examplelib";
files = ["example_wrap.c","example.o"];
libs = [];
table = ["gcd","_wrap_gcd";"Foo_set","_wrap_Foo_set";"Foo_get","_wrap_Foo_get";];
ilib_build(ilib_name,table,files,libs);
</pre></div>
<p>
"ilib_name" is the name of the lib we want to build. "table" contains the name of the C file and its wrapper file. "files" represent the .o file we want to compile, and"libs" is other libs we want to use.
</p>
<p>
"exec builder.sce" will produce *.so,and a file called "loader.sce" which contains how to load the module. Loading it into Scilab is then a matter of invoking
</p>
<div class="targetlang"><pre>Scilab:1&gt; exec loader.sce</pre></div>
<H3><a name="Scilab_nn6"></a>36.2.2 Using your module</H3>
<p>
Assuming all goes well, you will be able to do this:
<br>
</p>
<div class="targetlang"><pre>
Scilab:2&gt;gcd(4,6)
ans = 2
Scilab:3&gt;Foo_get
ans = 3
Scilab:4&gt;Foo_set(4);
Scilab:5&gt;Foo_get
ans = 4 </pre></div>
<H2><a name="Scilab_nn7"></a>36.3 A tour of basic C wrapping</H2>
<H3><a name="Scilab_nn8"></a>36.3.1 Modules</H3>
<p>
The SWIG module directive specifies the name of the Scilab module. If you want to load the module, you'll need a file called "loader.sce" which is usually generated by the command "exec builder.sce". The loader.sce looks as following:
</p>
<div class="targetlang"><pre>
// ------------------------------------------------------
// generated by builder.sce: Please do not edit this file
// ------------------------------------------------------
libexamplelib_path = get_file_path('loader.sce');
list_functions = [ 'gcd';
'Foo_set';
'Foo_get';
];
addinter(libexamplelib_path+'/libexamplelib.so','libexamplelib',list_functions);
// remove temp. variables on stack
clear libexamplelib_path;
clear list_functions;
clear get_file_path;
// ------------------------------------------------------
</pre></div>
<p>
After you run the command "exec loader.sce", you could use the module.
</pre>
<H3><a name="Scilab_nn9"></a>36.3.2 Functions</H3>
<p>
Global functions are wrapped as new Scilab built-in functions. For example,
</p>
<div class="code"><pre>&#037;module example
int fact(int n); </pre></div>
<p>
creates a built-in function <tt>fact(n)</tt> that works exactly like you think it does:
</p>
<div class="targetlang"><pre>Scilab:1&gt;fact(4)
ant=24 </pre></div>