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

Dirk Eddelbuettel edd at debian.org
Mon Nov 17 05:26:27 CET 2008


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.



More information about the R-devel mailing list