More examples
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@491 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
b3e124ac21
commit
e560c1d445
10 changed files with 326 additions and 41 deletions
17
Examples/perl5/constants/Makefile
Normal file
17
Examples/perl5/constants/Makefile
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
SRCS =
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT =
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' perl5
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myperl' INTERFACE='$(INTERFACE)' perl5_static
|
||||
|
||||
clean::
|
||||
rm -f *_wrap* *.o core *~ *.so *.pm myperl
|
||||
|
||||
24
Examples/perl5/constants/example.i
Normal file
24
Examples/perl5/constants/example.i
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
/* A few preprocessor macros */
|
||||
|
||||
#define ICONST 42
|
||||
#define FCONST 2.1828
|
||||
#define CCONST 'x'
|
||||
#define SCONST "Hello World"
|
||||
|
||||
/* This should work just fine */
|
||||
#define EXPR ICONST + 3*(FCONST)
|
||||
|
||||
/* This shouldn't do anything */
|
||||
#define EXTERN extern
|
||||
|
||||
/* Neither should this (BAR isn't defined) */
|
||||
#define FOO (ICONST + BAR)
|
||||
|
||||
/* The following statements also produce constants */
|
||||
const int iconst = 37;
|
||||
const double fconst = 3.14;
|
||||
|
||||
|
||||
27
Examples/perl5/constants/example.pl
Normal file
27
Examples/perl5/constants/example.pl
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# file: example.pl
|
||||
|
||||
use example;
|
||||
|
||||
print "ICONST = ", $example::ICONST, " (should be 42)\n";
|
||||
print "FCONST = ", $example::FCONST, " (should be 2.1828)\n";
|
||||
print "CCONST = ", $example::CCONST, " (should be 'x')\n";
|
||||
print "SCONST = ", $example::SCONST, " (should be 'Hello World')\n";
|
||||
print "EXPR = ", $example::EXPR, " (should be 48.5484)\n";
|
||||
print "iconst = ", $example::iconst, " (should be 37)\n";
|
||||
print "fconst = ", $example::fconst, " (should be 3.14)\n";
|
||||
|
||||
|
||||
if ($example::EXTERN) {
|
||||
print "EXTERN = ", example.EXTERN, " (Arg! This shouldn't print anything)\n";
|
||||
} else {
|
||||
print "EXTERN isn't defined (good)\n";
|
||||
}
|
||||
|
||||
if ($example::FOO) {
|
||||
print "FOO = ", example.FOO, "(Arg! This shouldn't print anything)\n";
|
||||
} else {
|
||||
print "FOO isn't defined (good)\n";
|
||||
}
|
||||
|
||||
|
||||
|
||||
55
Examples/perl5/constants/index.html
Normal file
55
Examples/perl5/constants/index.html
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:perl5:constants</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
<tt>SWIG/Examples/perl5/constants/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Wrapping C Constants</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
When SWIG encounters C preprocessor macros and C declarations that look like constants,
|
||||
it creates Perl5 variables with an identical value. Click <a href="example.i">here</a>
|
||||
to see a SWIG interface with some constant declarations in it.
|
||||
|
||||
<h2>Accessing Constants from Perl</h2>
|
||||
|
||||
Click <a href="example.pl">here</a> to see a script that prints out the values
|
||||
of the constants contained in the above file.
|
||||
|
||||
<h2>Key points</h2>
|
||||
|
||||
<ul>
|
||||
<li>The values of preprocessor macros are converted into Perl constants.
|
||||
<li>Types are inferred by syntax (e.g., "3" is an integer and "3.5" is a float).
|
||||
<li>Character constants such as 'x' are converted into Perl strings.
|
||||
<li>C string literals such as "Hello World" are converted into Perl strings.
|
||||
<li>Macros that are not fully defined are simply ignored. For example:
|
||||
<blockquote>
|
||||
<pre>
|
||||
#define EXTERN extern
|
||||
</pre>
|
||||
</blockquote>
|
||||
is ignored because SWIG has no idea what type of variable this would be.
|
||||
|
||||
<p>
|
||||
<li>Expressions are allowed provided that all of their components are defined. Otherwise, the constant is ignored.
|
||||
|
||||
<li>Certain C declarations involving 'const' are also turned into Perl constants.
|
||||
|
||||
<p>
|
||||
<li>The constants that appear in a SWIG interface file do not have to appear in any sort
|
||||
of matching C source file since the creation of a constant does not require linkage
|
||||
to a stored value (i.e., a value held in a C global variable or memory location).
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
||||
74
Examples/perl5/index.html
Normal file
74
Examples/perl5/index.html
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:perl5</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
<H1>SWIG Perl Examples</H1>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
The following examples illustrate the use of SWIG with Perl.
|
||||
|
||||
<ul>
|
||||
<li><a href="simple/index.html">simple</a>. A minimal example showing how SWIG can
|
||||
be used to wrap a C function and a global variable.
|
||||
<li><a href="constants/index.html">constants</a>. This shows how preprocessor macros and
|
||||
certain C declarations are turned into constants.
|
||||
</ul>
|
||||
|
||||
<h2>Compilation Issues</h2>
|
||||
|
||||
<ul>
|
||||
<li>To create a Perl extension, SWIG is run with the following options:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
% swig -perl5 interface.i
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<li>The compilation of examples is done using the file <tt>Example/Makefile</tt>. This
|
||||
makefile performs a manual module compilation which is platform specific. Typically,
|
||||
the steps look like this (Linux):
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
% swig -perl5 interface.i
|
||||
% gcc -fpic -c -Dbool=char -I/usr/lib/perl5/5.00503/i386-linux/CORE interface_wrap.c
|
||||
% gcc -shared interface_wrap.o $(OBJS) -o interface.so
|
||||
% perl
|
||||
use interface;
|
||||
...
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<li>The politically "correct" way to compile a Perl extension module is to use MakeMaker
|
||||
and related tools (especially if you are considering third-party distribution). Consult
|
||||
a book such as Advanced Perl Programming for details.
|
||||
|
||||
</ul>
|
||||
|
||||
<h2>Compatibility</h2>
|
||||
|
||||
The examples have been extensively tested on the following platforms:
|
||||
|
||||
<p>
|
||||
<ul>
|
||||
<li>Linux
|
||||
<li>Solaris
|
||||
</ul>
|
||||
|
||||
<p>
|
||||
Due to wide variations in the Perl C API and differences between versions such as the ActivePerl release for Windows,
|
||||
the code generated by SWIG is extremely messy. We have made every attempt to maintain compatibility with
|
||||
many Perl releases going as far back as 5.003 and as recent as 5.6. However, your mileage may vary.
|
||||
If you experience a problem, please let us know by
|
||||
sending a message to <a href="mailto:swig-dev@cs.uchicago.edu">swig-dev@cs.uchicago.edu</a>.
|
||||
Better yet, send us a patch.
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
|
|
@ -1 +0,0 @@
|
|||
Simple example from users manual.
|
||||
|
|
@ -1,24 +1,18 @@
|
|||
/* Simple example from documentation */
|
||||
/* File : example.c */
|
||||
|
||||
#include <time.h>
|
||||
/* A global variable */
|
||||
double Foo = 3.0;
|
||||
|
||||
double My_variable = 3.0;
|
||||
|
||||
/* Compute factorial of n */
|
||||
int fact(int n) {
|
||||
if (n <= 1) return 1;
|
||||
else return n*fact(n-1);
|
||||
}
|
||||
|
||||
/* Compute n mod m */
|
||||
int my_mod(int n, int m) {
|
||||
return (n % m);
|
||||
/* Compute the greatest common divisor of positive integers */
|
||||
int gcd(int x, int y) {
|
||||
int g;
|
||||
g = y;
|
||||
while (x > 0) {
|
||||
g = x;
|
||||
x = y % x;
|
||||
y = g;
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
|
||||
char *get_time() {
|
||||
long ltime;
|
||||
time(<ime);
|
||||
return ctime(<ime);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,5 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
%{
|
||||
/* Put headers and other declarations here */
|
||||
%}
|
||||
|
||||
|
||||
extern double My_variable;
|
||||
extern int fact(int);
|
||||
%name(mod) extern int my_mod(int n, int m);
|
||||
extern char *get_time();
|
||||
extern int gcd(int x, int y);
|
||||
extern double Foo;
|
||||
|
|
|
|||
|
|
@ -2,22 +2,22 @@
|
|||
# Perl5 script for testing simple example
|
||||
|
||||
use example;
|
||||
package example;
|
||||
|
||||
print get_time();
|
||||
print "My Variable = $My_variable","\n";
|
||||
for ($i = 0; $i < 14; $i++) {
|
||||
$n = fact($i);
|
||||
print "$i factorial is $n\n";
|
||||
}
|
||||
# Call our gcd() function
|
||||
|
||||
for ($i = 1; $i < 250; $i++) {
|
||||
for ($j = 1; $j < 250; $j++) {
|
||||
$n = mod($i,$j);
|
||||
$My_variable = $My_variable + $n;
|
||||
}
|
||||
}
|
||||
$x = 42;
|
||||
$y = 105;
|
||||
$g = example::gcd($x,$y);
|
||||
print "The gcd of $x and $y is $g\n";
|
||||
|
||||
print "My_variable = ", $My_variable, "\n";
|
||||
# Manipulate the Foo global variable
|
||||
|
||||
# Output its current value
|
||||
print "Foo = $example::Foo\n";
|
||||
|
||||
# Change its value
|
||||
$example::Foo = 3.1415926;
|
||||
|
||||
# See if the change took effect
|
||||
print "Foo = $example::Foo\n";
|
||||
|
||||
|
|
|
|||
101
Examples/perl5/simple/index.html
Normal file
101
Examples/perl5/simple/index.html
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:perl5:simple</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
|
||||
<tt>SWIG/Examples/perl5/simple/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Simple Perl5 Example</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
This example illustrates how you can hook Perl to a very simple C program containing
|
||||
a function and a global variable.
|
||||
|
||||
<h2>The C Code</h2>
|
||||
|
||||
Suppose you have the following C code:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
/* File : example.c */
|
||||
|
||||
/* A global variable */
|
||||
double Foo = 3.0;
|
||||
|
||||
/* Compute the greatest common divisor of positive integers */
|
||||
int gcd(int x, int y) {
|
||||
int g;
|
||||
g = y;
|
||||
while (x > 0) {
|
||||
g = x;
|
||||
x = y % x;
|
||||
y = g;
|
||||
}
|
||||
return g;
|
||||
}
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<h2>The SWIG interface</h2>
|
||||
|
||||
Here is a simple SWIG interface file:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
/* File: example.i */
|
||||
%module example
|
||||
|
||||
extern int gcd(int x, int y);
|
||||
extern double Foo;
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<h2>Compilation</h2>
|
||||
|
||||
<ol>
|
||||
<li><tt>swig -perl5 <a href="example.i">example.i</a></tt>
|
||||
<p>
|
||||
<li>This produces two files: <tt><a href="example_wrap.c">example_wrap.c</a></tt> and <tt><a href="example.pm">example.pm</a></tt>.
|
||||
<p>
|
||||
<li>Compile <tt><a href="example_wrap.c">example_wrap.c</a></tt> and <tt><a href="example.c">example.c</a></tt>
|
||||
to create the extension <tt>example.so</tt>.
|
||||
</ol>
|
||||
|
||||
<h2>Using the extension</h2>
|
||||
|
||||
Click <a href="example.pl">here</a> to see a script that calls our C functions from Perl.
|
||||
|
||||
<h2>Key points</h2>
|
||||
|
||||
<ul>
|
||||
<li>Use the <tt>use</tt> statement to load your extension module from Perl. For example:
|
||||
<blockquote>
|
||||
<pre>
|
||||
use example;
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<li>C functions work just like Perl functions. For example:
|
||||
<blockquote>
|
||||
<pre>
|
||||
$g = example::gcd(42,105);
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
<li>C global variables are accessed like ordinary Perl variables. For example:
|
||||
<blockquote>
|
||||
<pre>
|
||||
$a = $example::Foo;
|
||||
</pre>
|
||||
</blockquote>
|
||||
</ul>
|
||||
|
||||
<hr>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Add table
Add a link
Reference in a new issue