An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20081116/abefb8ca/attachment.pl>
SWIG with R and C++ STL
4 messages · charlie, Dirk Eddelbuettel, Whit Armstrong
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.
1 day later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20081118/ea37bd34/attachment.pl>
no all compilers support the export keyword. just put the template in a .h or .hpp file and include it in your .cpp file. that should be enough. -Whit
On Tue, Nov 18, 2008 at 2:50 PM, charlie <charlie.xia.fdu at gmail.com> wrote:
Thanks guys for all your kind heartedness. Yeah, I got messed up with a lot of things in my previous program, like missing the definition of one function and etc. I finally discovered a good example on: http://www.opensource.apple.com/darwinsource/Current/swig-4/swig/Examples/java/funcptr/index.html. And I was able to make a workable example as following: /* test.i */ %module test %{ #include "test.h" %} %include "test.h" %constant B (*F)( const A&)=f; //let a=A(), b=B() %template(ia) ex1<A>; // from Doc, works %template(jab) ex2<A, B>; // jab(a,b) works %template(kab) ex3<A, B>; // kab(a,b,1) works %template(lab) ex4<A, B>; // lab(a,b,1,F) works %template(fab) ex5<A, B>; // fab(a,b,1,F) ultimate goal /* test.h */ #include<iostream> using namespace std; class A { }; class B { }; class E { }; B f( const A& ) { cout<<"This is a function map A to B"<<endl; return *(new B); }; typedef B (*fp)( const A& ); template<class T> E ex1(T a, T b) { cout<<"ex1, one parameter template"<<endl; return *(new E); } // NOTE: simple template function template<class T, class S> E ex2(T t, S s) { cout<<"ex2, two parameters template"<<endl; return *(new E); } // NOTE: this extends the one above with two template parameters template<class T, class S> E ex3(T t, S s, int k) { cout<<"ex3, two parameters template plus an int argument"<<endl; return *(new E); } // NOTE: this extends the one above with additional integer argument template<class T, class S> E ex4(T t, S s, int k, fp gp) { cout<<"ex4, two parameters template, an int argument and an func ptr argument"<<endl; A a=A(); B b=gp(a); return *(new E); } // NOTE: this extends the one above with one more function poiter argument template <class C, class D> E ex5(C c, D& d, int n, D (*g)(const C&)) { cout<<"ex5, two parameters template, an int argument and a template func ptr argument"<<endl; g(c); return *(new E); } // NOTE: this temaplate function TF takes a template function poiter as argument, our ultimate goal And currently I got my program work now. But the ex. in 5.4.9 still seems not working. Anyway, so far good for me now. Maybe one last thing, currently I need to put the definition and declaration of a template function all in one file, otherwise I got undefined symbol error. How can I separate them into a .h and .cpp file? The 'export' keyword which suggested in TCPL seems do't work with gcc. Any comments? Thanks a bunch already! Charlie