I am trying to get a dll compiled for use with dyn.load. I use R.1.7.1
under Windows.
I have tried the following trivial example based on the "Writing R
extensions" manual.
rtest.h
--------
class X {
public:
X ();
~X ();
void Give7(double*);
};
class Y {
public: Y (); ~Y ();
};
rtest.cpp
---------
#include <iostream.h>
#include "rtest.h"
static Y y;
extern "C" {
void X_main () {
X x;
}
}
X::X() { std::cout << "construct X" << std::endl; }
X::~X() { std::cout << "destruct X" << std::endl; }
Y::Y() { std::cout << "construct Y" << std::endl; }
Y::~Y() { std::cout << "destruct Y" << std::endl; }
//silly function to test returning a constant double
void X::Give7(double * a)
{
*a=7;
}
---- end rtest.cpp
Then I create the dll using the following DOS command
Rcmd SHLIB rtest.cpp
(I have all the perl, MINGW and related stuff set up as
per the instructions at http://www.stats.ox.ac.uk/pub/Rtools/)
Then in R, I type the following.> x <- 3
> dyn.load("rtest.dll")
So far, so good.
Then...PROBLEM 1> .C("Give7",x)
Error in .C("Give7", x) : C/Fortran function name not in load table
How do you convince the compiler to use the name "Give7" as an entry
point?
And PROBLEM 2...
I checked the DLL and the names have been decorated.
If I use the decorated name I get the following:> .C("_ZN1X5Give7EPd",x)
[[1]]
[1] 3
For some reason, it is not altering x as it should.
What am I doing wrong?
Thanks.
Rob Hyndman
___________________________________________________
Rob J Hyndman
Associate Professor & Director of Consulting
Department of Econometrics & Business Statistics
Monash University, VIC 3800, Australia.
http://www-personal.buseco.monash.edu.au/~hyndman/
On Mon, 07 Jul 2003 13:49:01 +1000, you wrote:>//silly function to test returning a constant double >void X::Give7(double * a) >{ > *a=7; >} >---- end rtest.cpp > >Then I create the dll using the following DOS command > Rcmd SHLIB rtest.cpp >(I have all the perl, MINGW and related stuff set up as >per the instructions at http://www.stats.ox.ac.uk/pub/Rtools/) > >Then in R, I type the following. >> x <- 3 >> dyn.load("rtest.dll") >So far, so good. > >Then...PROBLEM 1 >> .C("Give7",x) >Error in .C("Give7", x) : C/Fortran function name not in load tableA call to Give7 needs to pass both an instance of X and a. You're only passing a, because R doesn't know anything about instances of X. Generally speaking, R can only call standard C functions, not C++ methods. Your example seems to be based on the Writing R Extensions section 4.6 example, but you miss the last part of it: R can only call X_main, not X::Give7. Duncan Murdoch> >How do you convince the compiler to use the name "Give7" as an entry >point? > >And PROBLEM 2... >I checked the DLL and the names have been decorated. >If I use the decorated name I get the following: >> .C("_ZN1X5Give7EPd",x) >[[1]] >[1] 3 > >For some reason, it is not altering x as it should. > >What am I doing wrong? > >Thanks. >Rob Hyndman >___________________________________________________ >Rob J Hyndman >Associate Professor & Director of Consulting >Department of Econometrics & Business Statistics >Monash University, VIC 3800, Australia. >http://www-personal.buseco.monash.edu.au/~hyndman/ > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help