[R] interfacing C++ using .Call
Prof Brian Ripley
ripley at stats.ox.ac.uk
Tue Feb 17 18:44:43 CET 2004
On Tue, 17 Feb 2004, Sundar Dorai-Raj wrote:
> Hi folks,
> I apologise if this is in the documentation somewhere, but I can't
> seem to find it. I also did a search of CRAN without any success. I'm
> using R-1.8.1 (pre-compiled) on Windows 2000 with Rtools and mingw 2.0.0
> (which includes gcc/g++ 3.2).
>
> I'm trying to link some C++ code from another application to R using the
> .Call interface and am experiencing some problems. I was able to compile
> and link the example from Section 4.6 in R-exts.pdf without any
> difficulty. But if I take a more complicated example that uses SEXP I'm
> able to compile but not link. Here's an example:
>
> Take the "lapply2" code on page 49 of R-exts.pdf and save to "test.c".
> Be sure to add
>
> #include <R.h>
> #include <Rdefines.h>
That should be <Rinternals.h>, as in R-exts.c in the sources.
> to the top of the file.
>
> Now at the command line type:
>
> > Rcmd SHLIB test.c
> making test.d from test.c
> gcc -Id:/R/rw1081/src/include -Wall -O2 -c test.c -o test.o
> ar cr test.a *.o
> ranlib test.a
> gcc --shared -s -o test.dll test.def test.a
> -Ld:/R/rw1081/src/gnuwin32 -lg2c -lR
>
> Works fine (though I haven't tested in R but at least the dll is
> created). Now remove all files except "test.c" and rename to "test.cpp".
>
> > Rcmd SHLIB test.cpp
> making test.d from test.cpp
> g++ -Id:/R/rw1081/src/include -Wall -O2 -c test.cpp -o test.o
> ar cr test.a *.o
> ranlib test.a
> g++ --shared -s -o test.dll test.def test.a
> -Ld:/R/rw1081/src/gnuwin32 -lg2c -lR
> test.a(test.o.b)(.text+0x61):test.cpp: undefined reference to
> `Rf_length(SEXPREC*)'
> test.a(test.o.b)(.text+0x6e):test.cpp: undefined reference to
> `Rf_isNewList(SEXPREC*)'
> test.a(test.o.b)(.text+0x82):test.cpp: undefined reference to
> `Rf_isFunction(SEXPREC*)'
> test.a(test.o.b)(.text+0x99):test.cpp: undefined reference to
> `Rf_isEnvironment(SEXPREC*)'
> test.a(test.o.b)(.text+0xb7):test.cpp: undefined reference to
> `Rf_lang2(SEXPREC*, SEXPREC*)'
> test.a(test.o.b)(.text+0xc2):test.cpp: undefined reference to
> `Rf_protect(SEXPREC*)'
> test.a(test.o.b)(.text+0xcf):test.cpp: undefined reference to
> `Rf_allocVector(unsigned, int)'
> test.a(test.o.b)(.text+0xda):test.cpp: undefined reference to
> `Rf_protect(SEXPREC*)'
> test.a(test.o.b)(.text+0xf5):test.cpp: undefined reference to
> `Rf_getAttrib(SEXPREC*, SEXPREC*)'
> test.a(test.o.b)(.text+0x107):test.cpp: undefined reference to
> `Rf_setAttrib(SEXPREC*, SEXPREC*, SEXPREC*)'
> test.a(test.o.b)(.text+0x113):test.cpp: undefined reference to
> `Rf_unprotect(int)'
> test.a(test.o.b)(.text+0x129):test.cpp: undefined reference to
> `VECTOR_ELT(SEXPREC*, int)'
> test.a(test.o.b)(.text+0x132):test.cpp: undefined reference to
> `SETCADR(SEXPREC*, SEXPREC*)'
> test.a(test.o.b)(.text+0x13e):test.cpp: undefined reference to
> `Rf_eval(SEXPREC*, SEXPREC*)'
> test.a(test.o.b)(.text+0x14a):test.cpp: undefined reference to
> `SET_VECTOR_ELT(SEXPREC*, int, SEXPREC*)'
> make: *** [test.dll] Error 1
>
> Adding extern "C" {} does not help the problem. Can anybody replicate
> this? Is there something I'm missing?
You need to do that. If I wrap the whole file (including the #includes)
in that, it works for me.
--
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