add support for constants
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-sploving@11251 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
25f17ae463
commit
4e9cbd8a7c
15 changed files with 470 additions and 142 deletions
|
|
@ -23,6 +23,7 @@
|
|||
<li><a href="#Scilab_nn8">Modules</a>
|
||||
<li><a href="#Scilab_nn9">Functions</a>
|
||||
<li><a href="#scilab_nn10">Global variables</a>
|
||||
<li><a href="#Scilab_nn11">Constants</a>
|
||||
</ul>
|
||||
</ul>
|
||||
</div>
|
||||
|
|
@ -66,7 +67,7 @@ To build an Scilab module, run SWIG using the <tt>-scilab</tt> option.
|
|||
<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.
|
||||
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>
|
||||
|
|
@ -89,7 +90,7 @@ $ ./scilab
|
|||
<p>
|
||||
where builder.sce is the interface file generated by the swig. It looks like the following:
|
||||
</p>
|
||||
<div class="shell"><pre>
|
||||
<div class="code"><pre>
|
||||
ilib_name = "examplelib";
|
||||
files = ["example_wrap.c","example.o"];
|
||||
libs = [];
|
||||
|
|
@ -97,16 +98,21 @@ 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>ilib_build(lib_name,table,files,libs) is used to create shared libraries and to generate a loader file which can be used to dynamically load the shared library into Scilab with addinter.
|
||||
</p>
|
||||
|
||||
<ul>
|
||||
<li><tt><b>ilib_name</b></tt>: a character string, the generic name of the library without path and extension.</li>
|
||||
<li><tt><b>files</b></tt>: string matrix giving objects files needed for shared library creation.</li>
|
||||
<li><tt><b>libs</b></tt>: string matrix giving extra libraries needed for shred library creation.</li>
|
||||
<li><tt><b>table</b></tt>: two column string matrix giving the table of pairs 'scilab-name', 'interface name'.</li>
|
||||
</ul>
|
||||
|
||||
<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> exec loader.sce</pre></div>
|
||||
<div class="shell"><pre>Scilab:1> exec loader.sce</pre></div>
|
||||
|
||||
<H3><a name="Scilab_nn6"></a>36.2.2 Using your module</H3>
|
||||
|
||||
|
|
@ -119,9 +125,12 @@ Assuming all goes well, you will be able to do this:
|
|||
<div class="targetlang"><pre>
|
||||
Scilab:2>gcd(4,6)
|
||||
ans = 2
|
||||
|
||||
Scilab:3>Foo_get
|
||||
ans = 3
|
||||
|
||||
Scilab:4>Foo_set(4);
|
||||
|
||||
Scilab:5>Foo_get
|
||||
ans = 4 </pre></div>
|
||||
|
||||
|
|
@ -135,7 +144,7 @@ ans = 4 </pre></div>
|
|||
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>
|
||||
<div class="code"><pre>
|
||||
// ------------------------------------------------------
|
||||
// generated by builder.sce: Please do not edit this file
|
||||
// ------------------------------------------------------
|
||||
|
|
@ -153,6 +162,15 @@ clear get_file_path;
|
|||
// ------------------------------------------------------
|
||||
|
||||
</pre></div>
|
||||
<p>addinter (files,spname,fcts) performs incremental linking of a compiled C new Scilab interface routine.
|
||||
</p>
|
||||
<ul>
|
||||
<li><tt><b>files</b></tt>: a character string or a vector of character string contain object files used to define the new Scilab interface routine (interface code, user routines or libraries, system libraries).</li>
|
||||
<li><tt><b>spname</b></tt>: a character string. Name of interface routine entry point.</li>
|
||||
<li><tt><b>fcts</b></tt>: vector of character strings. The name of new Scilab function implemented in the new interface.</li>
|
||||
</ul>
|
||||
|
||||
|
||||
<p>
|
||||
After you run the command "exec loader.sce", you could use the module.
|
||||
</pre>
|
||||
|
|
@ -176,16 +194,55 @@ ant=24 </pre></div>
|
|||
<H3><a name="scilab_nn10"></a>27.3.3 Global variables</H3>
|
||||
|
||||
<p>
|
||||
To expose variables, SWIG actually generates two functions, to get and set the value. In this case, Foo_set and Foo_set would be generated. SWIG then automatically calls these functions when you get and set the variable-- in the former case creating a local copy in the interpreter of the C variables, and in the latter case copying an interpreter variables onto the C variable.
|
||||
To expose variables, SWIG actually generates two functions, to get and set the value. In this case, Foo_set and Foo_get would be generated. SWIG then automatically calls these functions when you get and set the variable-- in the former case creating a local copy in the interpreter of the C variables, and in the latter case copying an interpreter variables onto the C variable.
|
||||
</p>
|
||||
|
||||
<div class="targetlang"><pre>scilab:1> exec loader.sce;
|
||||
scilab:2> c=Foo
|
||||
c = 3
|
||||
scilab:3> Foo=4;
|
||||
scilab:2> c=Foo_get();
|
||||
|
||||
scilab:3> Foo_set(4);
|
||||
|
||||
scilab:4> c
|
||||
c = 3
|
||||
scilab:5> Foo
|
||||
|
||||
scilab:5> Foo_get()
|
||||
ans = 4</pre></div>
|
||||
<H3><a name="Scilab_nn11"></a>27.3.4 Constants</H3>
|
||||
|
||||
|
||||
<p>
|
||||
C constants are not really constant in Scilab. They are actually just a copy of the value into the Scilab interpreter. Therefore they can be changed just as any other value. For example given some constants:
|
||||
</p>
|
||||
|
||||
<div class="code"><pre>%module example
|
||||
%constant int ICONST=42;
|
||||
#define SCONST "Hello World"
|
||||
</pre></div>
|
||||
|
||||
<p>
|
||||
This is 'effectively' converted into the following code in the wrapper file:
|
||||
</p>
|
||||
|
||||
<div class="code"><pre>....
|
||||
const int ICONST=42;
|
||||
const char * SCONST="Hello World";
|
||||
....
|
||||
int ICONST_get (char *fname,unsigned long fname_len) {..}
|
||||
int SCONST_get (char *fname,unsigned long fname_len) {..}
|
||||
.... </pre></div>
|
||||
<p>It is easy to use the C constants as global variables:</p>
|
||||
|
||||
<div class="targetlang"><pre>
|
||||
scilab:1> ICONST
|
||||
ant = 42
|
||||
|
||||
scilab:2> SCONST
|
||||
ant= Hello world
|
||||
|
||||
scilab:3> c=SCONST()
|
||||
c = Hello World
|
||||
</pre></div>
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue