[Rcpp-devel] Create and access several instances of a C++ class from R

Dirk Eddelbuettel edd at debian.org
Sat May 14 18:17:43 CEST 2011


Soeren,

On 13 May 2011 at 20:07, soeren.vogel at uzh.ch wrote:
| Compilation with R CMD CHECK FOO fails with the following error in 00install.out:
| 
| Error in dyn.load(file, DLLpath = DLLpath, ...) : 
|   unable to load shared object '/Users/sovo/GUTS/FOO.Rcheck/FOO/libs/i386/FOO.so':
|   dlopen(/Users/sovo/GUTS/FOO.Rcheck/FOO/libs/i386/FOO.so, 6): Symbol not found: __ZN3FOOC1Ev
|   Referenced from: /Users/sovo/GUTS/FOO.Rcheck/FOO/libs/i386/FOO.so
|   Expected in: flat namespace
| 
| The (original) class and its functions compile fine with R CMD SHLIB. So we guess that this error has something to do with the Rcpp modules implementation.

It took me a few minutes with trial and error, but essentially the mistake
seems to have been this:

| > | 		FOO();
| > | 		~FOO();

You declared the constructor and deconstructor but there was no code
anywhere.  A pair of empty braces ' {}; '  is all it takes.  

In the process, I renamed things from 'foo' (just to be plain) to 'Baz'.  So
the file is now

-----------------------------------------------------------------------------
// baz_module.cpp 
#include <Rcpp.h>

// from Baz.h
class Baz
{
private:
  double dtau;
  std::vector<double> D, ee, ff, S;

public:
  int M;
  std::vector<double> C, t, s, par;
  std::vector<int> y;

  Baz() {};
  ~Baz() {};

  double do_bar(std::vector<double> z);
};

// from Baz.cpp
double Baz::do_bar(std::vector<double> z) {
  // whatever it does
  return 42.0;			// need to return something here
}

RCPP_MODULE(baz){
  using namespace Rcpp ;
  class_<Baz>( "Baz" )
    .constructor()
    .field( "M" , &Baz::M )
    .field( "C" , &Baz::C )
    .method( "do_bar", &Baz::do_bar )
    ;
}
-----------------------------------------------------------------------------

and with the line 

    RcppModules: yada, baz

we get this module loaded:


R> library(mypackage)
R> Baz
C++ class 'Baz' <0x1c37db0>
Constructors:
    Baz()

Fields: 
    std::vector<double, std::allocator<double> > C
    int M

Methods: 
     double do_bar(std::vector<double, std::allocator<double> >)  
           
R> 
R> bb <- new(Baz)
R> bb
C++ object <0x18c8f90> of class 'Baz' <0x1c37db0>
R> bb$M <- 12
R> print(bb$M)
[1] 12
R> print(bb$C)
numeric(0)
R> bb$C <- seq(1.1, 5.5, by=1.1)
R> bb$C
[1] 1.1 2.2 3.3 4.4 5.5
R> R> print(bb$do_bar(1:4))
[1] 42
R>


I hope you can take it from here. 

Cheers, Dirk

-- 
Gauss once played himself in a zero-sum game and won $50.
                      -- #11 at http://www.gaussfacts.com


More information about the Rcpp-devel mailing list