[R] simple example of C interface to R
Prof Brian Ripley
ripley at stats.ox.ac.uk
Tue Feb 8 22:42:59 CET 2005
Did none of you notice the Makefile in that directory?
On Tue, 8 Feb 2005, Huntsinger, Reid wrote:
> Rtest.o is just an object file, and the second call to gcc is really a call
> to ld and creates Rtest.so, a shared library. Neither is executable.
> Probably you want to compile Rtest.c into an executable; it does have the
> "main" function after all. Perhaps
>
> gcc -L/usr/local/lib -o Rtest Rtest.o -L/usr/lib/R/bin -lR
Should be -L`R RHOME`/lib for R>=2.0.0 (and that Makefile needs updating).
>From ONEWS
o The dynamic libraries libR and libRlapack are now installed in
R_HOME/lib rather than R_HOME/bin.
You may also need to set LD_LIBRARY_PATH: I do to include /usr/local/lib.
> would work.
>
> Reid Huntsinger
>
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of jbdunsmo at utmb.edu
> Sent: Tuesday, February 08, 2005 12:14 PM
> To: Rau, Roland; r-help at stat.math.ethz.ch
> Subject: Re: [R] simple example of C interface to R
>
>
> On Tue, Feb 08, 2005 at 10:55:01AM +0100, Rau, Roland wrote:
>>
>> do you know already the page of Roger D. Peng?
>> He has a document entitled "An Introduction to the .C Interface to R".
>> It is located at:
>> http://www.biostat.jhsph.edu/~rpeng/docs/interface.pdf
>>
>
> thanks. that's a nice tutorial, but what i'd really like to do is
> write a program with R embedded in it (and not have to load the
> interpreter). i found some documentation for doing that here:
> http://cran.r-project.org/doc/manuals/R-exts.html#Embedding-R-under-Unix_002
> dalikes
>
> now i'm going through the tests/Embedding directory (included with the
> R source distribution).
>
> i've gotten Rtest.c to compile with:
>
> $ R CMD SHLIB Rtest.c
> gcc -I/usr/lib/R/include -I/usr/local/include -D__NO_MATH_INLINES
> -mieee-fp -fPIC -march=pentium4 -O3 -pipe -fomit-frame-pointer -c Rtest.c
> -o Rtest.o
> gcc -shared -L/usr/local/lib -o Rtest.so Rtest.o -L/usr/lib/R/bin -lR
>
> then when i try to run Rtest.o, i get:
>
> -/bin/bash: ./Rtest.o: cannot execute binary file
>
> Rtest.c contains:
>
> #include "embeddedRCall.h"
>
> int
> main(int argc, char *argv[])
> {
> eval_R_command("print", argc, argv);
> return(0);
> }
>
> embeddedRCall.h contains:
>
> #ifndef EMBEDDED_R_CALL_H
> #define EMBEDDED_R_CALL_H
>
> #include <Rinternals.h>
>
> int eval_R_command(const char *funcName, int argc, char *argv[]);
> SEXP Test_tryEval(SEXP expression, int *errorOccurred);
> void init_R(int argc, char **argv);
>
> #endif
>
> i also get the "cannot execute binary file" error when trying to
> compile and run the following code from the documentation:
>
> int Rf_initEmbeddedR(int argc, char **argv)
> { /* This is already compiled into R */
> Rf_initialize_R(argc, argv);
> setup_Rmainloop();
> return(1);
> }
>
> int main(int ac, char **av)
> {
> /* do some setup */
> // Rf_initEmbeddedR(argc, argv);
> /* do some more setup */
>
> char *argv[]= {"REmbeddedPostgres", "--gui=none", "--silent"};
> Rf_initEmbeddedR(sizeof(argv)/sizeof(argv[0]), argv);
>
> /* submit some code to R, which is done interactively via
> run_Rmainloop();
> */
> end_Rmainloop();
> return 0;
> }
>
>
> what am i doing wrong?
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list