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

2008-11-18 Thread Whit Armstrong
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 <[EMAIL PROTECTED]> 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; // from Doc, works
> %template(jab) ex2; // jab(a,b) works
> %template(kab) ex3; // kab(a,b,1) works
> %template(lab) ex4; // lab(a,b,1,F) works
> %template(fab) ex5; // fab(a,b,1,F) ultimate goal
>
> /* test.h */
>
> #include
> using namespace std;
>
> class A { };
> class B { };
> class E { };
> B f( const A& ) { cout<<"This is a function map A to B"< B); };
> typedef B (*fp)( const A& );
>
> template E ex1(T a, T b) { cout<<"ex1, one parameter
> template"< // NOTE: simple template function
>
> template E ex2(T t, S s) { cout<<"ex2, two parameters
> template"< // NOTE: this extends the one above with two template parameters
>
> template E ex3(T t, S s, int k) {
>   cout<<"ex3, two parameters template plus an int argument"< *(new E); }
> // NOTE: this extends the one above with additional integer argument
>
> template E ex4(T t, S s, int k, fp gp) {
>   cout<<"ex4, two parameters template, an int argument and an func ptr
> argument"<   A a=A(); B b=gp(a); return *(new E); }
> // NOTE: this extends the one above with one more function poiter argument
>
> template   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"<   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
>
>
>
>

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


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

2008-11-18 Thread charlie
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; // from Doc, works
%template(jab) ex2; // jab(a,b) works
%template(kab) ex3; // kab(a,b,1) works
%template(lab) ex4; // lab(a,b,1,F) works
%template(fab) ex5; // fab(a,b,1,F) ultimate goal


/* test.h */
#include
using namespace std;

class A { };
class B { };
class E { };
B f( const A& ) { cout<<"This is a function map A to B"< E ex1(T a, T b) { cout<<"ex1, one parameter
template"< E ex2(T t, S s) { cout<<"ex2, two parameters
template"< E ex3(T t, S s, int k) {
  cout<<"ex3, two parameters template plus an int argument"< E ex4(T t, S s, int k, fp gp) {
  cout<<"ex4, two parameters template, an int argument and an func ptr
argument"<  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"

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

2008-11-16 Thread Dirk Eddelbuettel

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:

[EMAIL PROTECTED]:~/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);
}



[EMAIL PROTECTED]:~/src/progs/swig/R> cat example.i
/* File : example.i */
%module example
%{
/* Put headers and other declarations here */
extern double My_variable;
extern intfact(int);
extern intmy_mod(int n, int m);
%}

extern double My_variable;
extern intfact(int);
extern intmy_mod(int n, int m);


[EMAIL PROTECTED]:~/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))


[EMAIL PROTECTED]:~/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


./[EMAIL PROTECTED]:~/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
[EMAIL PROTECTED]:~/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.

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


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

2008-11-16 Thread charlie
On Thu, Nov 6, 2008 at 11:29 AM, Whit Armstrong <[EMAIL PROTECTED]>wrote:

> did you wrap your function prototype in extern "C" ?
>
> -Whit
>
>
>
Hi Whit,

Thanks for replying my question. I already gave up my own code and now I am
trying with the examples from SWIG Doc 1.3. I can not even reproduce one of
their examples in section 5.4.9. I wonder if you can have a look at my code
and figure out what's wrong there for me. I used the extern "C" this time
and the details is as following:

-test.i--
%module test
%{
#include "test.h"
%}
%include "test.h"

%callback("%s_cb");
int myadd( int, int ); //myadd_cb
int mysub( int, int ); //mysub_cb
int mymul( int, int ); //mymul_cb
%nocallback;


---test.h--
extern "C"{
  int binary_op(int a, int b, int (*op)(int,int) );
  int myadd( int a, int b ) { return a+b; };
  int mysub( int a, int b ) { return a-b; };
  int mymul( int a, int b ) { return a*b; };

}

-
--error message--
> dyn.load("test.so")
Error in dyn.load("test.so") :
  unable to load shared library '/home/charlie/workspace/lla/test.so':
  /home/charlie/workspace/lla/test.so: undefined symbol: binary_op
--

--compiling message--
swig -v -r -c++ -o test_wrap.cpp test.i
LangSubDir: r
Search paths:
   ./
   ./swig_lib/r/
   /usr/local/share/swig/1.3.36/r/
   ./swig_lib/
   /usr/local/share/swig/1.3.36/
Preprocessing...
Starting language-specific parse...
Processing types...
C++ analysis...
Generating wrappers...
Type: p.f(int,int).int
Return type: int
R CMD SHLIB test.cpp test_wrap.cpp
make[1]: Entering directory `/home/charlie/workspace/lla'
g++ -I/usr/local/lib/R/include -I/usr/local/lib/R/include
-I/usr/local/include-fpic  -g -O2 -c test_wrap.cpp -o test_wrap.o
g++ -shared -L/usr/local/lib -o test.so test.o test_wrap.o
make[1]: Leaving directory `/home/charlie/workspace/lla'
--

Many thanks!

Charlie

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel