From edd at debian.org  Mon Nov 17 05:26:27 2008
From: edd at debian.org (Dirk Eddelbuettel)
Date: Sun, 16 Nov 2008 22:26:27 -0600
Subject: [Rd] Fwd:  Fwd: SWIG with R and C++ STL
In-Reply-To: <11c6cf4e0811161911y49ded77di905fc86ea630f4b5@mail.gmail.com>
References: <11c6cf4e0811041355v16321693hdd53d2376dc0a8c4@mail.gmail.com>
	<11c6cf4e0811061016r4dc114d8l261948c4512c1e12@mail.gmail.com>
	<8ec76080811061129p2e54125eqee948a31bb4a4de8@mail.gmail.com>
	<11c6cf4e0811161910v25ce5f30r71e891673612c078@mail.gmail.com>
	<11c6cf4e0811161911y49ded77di905fc86ea630f4b5@mail.gmail.com>
Message-ID: <18720.62067.394678.969716@ron.nulle.part>


I don't know about your examples from section 5.4.9 of the swig manual, but
the very basic SWIG example I have played with still works:

edd at ron:~/src/progs/swig/R> cat example.c
/* File : example.c */

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);
}



edd at ron:~/src/progs/swig/R> cat example.i
/* File : example.i */
%module example
%{
/* Put headers and other declarations here */
extern double My_variable;
extern int    fact(int);
extern int    my_mod(int n, int m);
%}

extern double My_variable;
extern int    fact(int);
extern int    my_mod(int n, int m);


edd at ron:~/src/progs/swig/R> cat testR.r
#!/usr/bin/r

dyn.load("example_wrap.so")
source("example_wrap.R")
print(fact(4))
print(My_variable_get())
print(my_mod(77,66))


edd at ron:~/src/progs/swig/R> cat makeR.sh
#!/bin/sh

set +e
set +u

rm -vf *.o *.so
swig -r example.i
PKG_LIBS="example.c" R CMD SHLIB example_wrap.c

#./testR.sh
./testR.r


./testR.redd at ron:~/src/progs/swig/R> ./makeR.sh
removed `example_wrap.o'
removed `example_wrap.so'
gcc -std=gnu99 -I/usr/share/R/include      -fpic  -g -O2 -c example_wrap.c -o example_wrap.o
gcc -std=gnu99 -shared  -o example_wrap.so example_wrap.o example.c  -L/usr/lib/R/lib -lR
Creating a new generic function for "print" in ".GlobalEnv"
[1] 24
[1] 3
[1] 11
edd at ron:~/src/progs/swig/R>

I haven't really tried anything much more complicated.  

Swig 1.3.36, R 2.8.0, g++ 4.3.2 on Debian testing.

Hope this helps,  Dirk


-- 
Three out of two people have difficulties with fractions.



[Rd] Fwd: Fwd: SWIG with R and C++ STL
Dirk Eddelbuettel