[R] Loading C++ libraries

Adrian Trapletti Adrian.Trapletti at wu-wien.ac.at
Thu Feb 18 14:19:05 CET 1999


Some time ago, I asked on this list, if it is possible to use C++
libraries
under R. In fact this seems to be very easy (at least under Redhat 5.2,
egcs C++):

e.g.
Suppose we have a library

R_main.cc:
-------------------------------
#include "X.hh"

extern "C" {

void R_main ()
{
  X x;
}

}
-------------------------------
X.hh
-------------------------------
class X
{
public:
  X ();
  ~X ();
};

class Y
{
public:
  Y ();
  ~Y ();
};
-------------------------------
X.cc:
-------------------------------
#include <iostream.h>
#include "X.hh"

static Y y;

X::X()
{
  cout << "constructor X" << endl;
}

X::~X()
{
  cout << "destructor X" << endl;
}

Y::Y()
{
  cout << "constructor Y" << endl;
}

Y::~Y()
{
  cout << "destructor Y" << endl;
}
--------------------------------
Compiled and linked by

45: g++ -c X.cc
46: g++ -c R_main.cc
47: g++ -shared -o X.so R_main.o X.o

Now starting R yields

R : Copyright 1999, The R Development Core Team
Version 0.63.2  (January 12, 1999)
...
Type    "q()" to quit R.

>dyn.load("X.so")
constructor Y
>.C("R_main")
constructor X
destructor X
> q()
Save workspace image? [y/n/c]: y
destructor Y
...

However linking has to be done with g++, since with the gnu ld

ld -shared -o X.so R_main.o X.o

__main() and hence, the constructor of the static variable Y are not
called.


Adrian

--
Adrian Trapletti, Vienna University of Economics and Business
Administration, Augasse 2-6, A-1090 Vienna, Austria
Phone: ++43 1 31336 4561, Fax: ++43 1 31336 708,
Email: adrian.trapletti at wu-wien.ac.at



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list