[R] dynamically loading C++

Douglas Bates bates at stat.wisc.edu
Mon Oct 19 19:18:58 CEST 1998

>>>>> "Adrian" == Adrian Trapletti <Adrian.Trapletti at wu-wien.ac.at> writes:

  Adrian> Is there a function ".C++(...)" for R, or does anybody know
  Adrian> how to dynamically load C++ functions into R?

Since the base language for R is C, I think it would be difficult to
call a C++ constructor or method directly from R.  R wouldn't know
about name mangling and things like that.

I have been successful dynamically loading C++ code on some systems by
ensuring that the function I will call is wrapped in

 extern "C" {


then using .C(...).  That is, make sure the function you will call
from R is a standard C function then have it construct C++ objects,
invoke methods, etc.

Here is an earlier message in which I described this

 From r-devel-owner  Wed Aug 20 20:26:44 1997
 Received: by franz.stat.wisc.edu
	 id m0x1FSW-000hqoC
	 (Debian Smail-3.2 1996-Jul-4 #2); Wed, 20 Aug 1997 13:26:20 -0500 (CDT)
 To: R-devel at stat.math.ethz.ch
 Subject: R-alpha: The Template Numerical Toolkit and R
 From: Douglas Bates <bates at stat.wisc.edu>
 Date: 20 Aug 1997 13:26:20 -0500
 Message-ID: <6r3eo4soc3.fsf at franz.stat.wisc.edu>

 I have tried linking C++ code using the Template Numerical Toolkit
 version 0.8.3 (see http://math.nist.gov/tnt/) with R-0.50-a3 (see
 http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html).  I expected this to
 be very difficult.  It was very easy.  I was pleasantly surprised.
 Nice work by the developers all around.

 A bit of background for the R crowd: TNT, being developed by Roldan
 Pozo at the National Institute for Standards and Technology, is the
 successor to lapack++

 As the name indicates, all the representations in TNT are templated.
 The advantages of using it with R would be that one could obtain the
 execution speed of compiled linear algebra code without the
 corresponding hassle of keeping track of all the dimensions associated
 with matrix representations in Fortran.  All of us really love to
 write those calls that require
  A, LDA, M, N
 for each matrix being passed but some of us in the baby boomer
 generation are getting on in age and we lose track of the calling
 sequence at about the 12th argument :-)

 TNT creates all matrices as copies of the original content.  Because
 it is written in C++, it gracefully handles the releasing of the
 storage when the identifier goes out of scope.

 Here is a sample run on a Linux system
 $ cat R_link.cc
 // An example of linking the Template Numerical Toolkit 0.8.3 with R-0.50 
 #include "tnt.h" 
 #include "vec.h" 
 #include "fmat.h" 

 extern "C" { 
   static double * 
     copy_contents(double *dest, Fortran_matrix<double> A) 
       double *dd = dest, *src = &A(1,1); 
       int nn = A.num_rows() * A.num_cols(); 

       while (nn--) { *dd++ = *src++; } 
       return dest; 

   void my_mat_sum(double * A, double * B, long int * dims) { 
       AA(dims[0], dims[1], A),  // creates a copy 
       BB(dims[0], dims[1], B), 
       CC = AA + BB; 

     cout << "A on input: " << AA << endl; 
     cout << "B on input: " << BB << endl; 
     cout << "Sum: " << CC << endl;  
     copy_contents(A, CC); 
 $ g++ -c -fpic R_link.cc 
 $ ld -shared -o R_link.so R_link.o -lstdc++ 
 $ R 

 R : Copyright 1997, Robert Gentleman and Ross Ihaka 
 Version 0.50 Alpha-3 (August 8, 1997) 

 R is free software and comes with ABSOLUTELY NO WARRANTY. 
 You are welcome to redistribute it under certain conditions. 
 Type "license()" for details. 

 [Previously saved workspace restored] 

 > A 
	    [,1]       [,2]       [,3] 
 [1,] -0.2713707 -1.1715863  0.8884861 
 [2,] -1.7103328  0.6957497 -0.7603538 
 > B 
	   [,1]      [,2]       [,3] 
 [1,] -1.573575 0.1554325 -1.1013854 
 [2,] -1.151815 0.1632211 -0.4710143 
 > A+B 
	   [,1]       [,2]       [,3] 
 [1,] -1.844946 -1.0161538 -0.2128993 
 [2,] -2.862148  0.8589708 -1.2313680 
 > dyn.load("R_link.so") 
 > .C("my_mat_sum", A, B, as.integer(dim(A)))[[1]] 
 A on input: 2 3 
 -0.271371 -1.17159 0.888486  
 -1.71033 0.69575 -0.760354  

 B on input: 2 3 
 -1.57357 0.155432 -1.10139  
 -1.15181 0.163221 -0.471014  

 Sum: 2 3 
 -1.84495 -1.01615 -0.212899  
 -2.86215 0.858971 -1.23137  

	   [,1]       [,2]       [,3] 
 [1,] -1.844946 -1.0161538 -0.2128993 
 [2,] -2.862148  0.8589708 -1.2313680 
 > q() 
 Save workspace image? [y/n/c]: n 

This is not guaranteed to work on all systems because C++ may need
functions that are not available in the standard C libraries.
Especially the constructors and the destructors in C++ tend to require
some special run-time routines.

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