[R] How to call R functions from a C++ program

Ramon Diaz-Uriarte ramon-diaz at teleline.es
Fri Nov 3 15:10:16 CET 2000


If you want to call certain functions, such as the random number generators,
that is easy. Get the sources, untar, and ./configure; then go to standalone
src/nmath/standalone, and read the README; if you want to call them as static
libraries, type make archive.  That will create the libRmath.a library.

Now, if you were using C, that would be all. But to use those functions from a
C++ program you need to modify Mathlib.h (in R_DIRECTORY/src/include/R_ext). 
Edit Mathlib.h and insert, before any of the function headers,

extern "C" {

(don't forget to add also the closing braket at the end of the file).
(The reason why you need to do this is explained, for instance, in Meyer's
"More effective C++", itme 34).

Thats it; you can now use the functions in Mathlib.h by giving the appropriate
paths to the library and header files.

For instance:

g++ -I/usr/lib/R-patched/src/include -L/usr/lib/R-patched/src/nmath/standalone
test1.cc -lRmath -lm



Ramon


On Fri, 03 Nov 2000, Nathapong Samlamjiag wrote:
> Dear R experts,
> 
> Could anyone give some concrete examples of how to call R functions from a 
> program written in C++?
> 
> Thanks in advance for your help.
> 
> Nathapong Samlamjiag
> _________________________________________________________________________
> Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.
> 
> Share information about yourself, create your own public profile at 
> http://profiles.msn.com.
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
-- 
Ramón Díaz-Uriarte
Triana 47
28016 Madrid
Spain

email:ramon-diaz at teleline.es
Phone: +-34-918-513-966
       +-34-657-186-407	







-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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