[R] interfacing C++ using .Call
Sundar Dorai-Raj
sundar.dorai-raj at pdf.com
Tue Feb 17 16:24:15 CET 2004
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>
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?
Regards,
Sundar
More information about the R-help
mailing list