swig.org and client routines

Dan Shearer dan at shearer.org
Wed Jul 14 22:27:19 GMT 1999


> On Wed, 14 Jul 1999, Dan Shearer wrote:
> 
> > http://www.swig.org/ might be a simple way for users of langauges such as
> 
> i am confused (this is easily done) by the explanation of swig on the web
> page.

1. I am not going to follow this up any further, I'm swamped with other
things right now. 

2. Look at http://www.swig.org/examples/simple/tutorial.html. That's what
I did and it worked perfectly. The following is the slightly complicated
version of what you can do cut from this page (there is a simpler way but
its best to understand what is going on first.) This thing is really
elegant.

You have a c file:

#include <time.h>
double My_variable = 3.0;

int fact(int n) {
    if (n <= 1) return 1;
    else return n*fact(n-1);
}        

int my_mod(int x, int y) {
    return (x%y);
}

char *get_time()
{
    time_t ltime;
    time(&ltime);
    return ctime(&ltime);
}          

you create a .i file....

/* example.i */
%module example
%{ 
  /* Put header files here (optional) */
%}

extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();

... and finally you create a perl module....

unix % swig -perl5 example.i
Making wrappers for Perl5  
unix % gcc -c example.c example_wrap.c -I/usr/lib/perl/solaris/5.003/CORE
unix % ld -G example.o example_wrap.o -o example.so

.....and use it!

unix % perl
use example;
print $example::My_variable,"\n";
print example::fact(5),"\n";
print example::get_time(),"\n";
<ctrl-d>
3.0
120
Sun Feb 11 23:01:07 1996
unix %  


Dan



More information about the samba-technical mailing list