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
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: ><snip>> Adding extern "C" {} does not help the problem. Can anybody replicate > this? Is there something I'm missing?Did you put extern "C" {} around the #include as well? Using R/doc/manual/R-exts.c and the driver in R/doc/manual/R-exts.R I don't have any problems if I rename R-exts.c to R-exts.cpp as long as the extern "C" {} surrounds the headers as well as the code. In my case (on OS X) the error when I don't do this comes at load time rather than compile time, and it's clear that the problem is C++ name mangling Error in dyn.load(x, as.logical(local), as.logical(now)) : unable to load shared library "/Users/thomas/R-exts.so": dlcompat: dyld: /usr/local/lib/R/bin/R.bin Undefined symbols: __Z10Rf_findVarP7SEXPRECS0_ __Z10Rf_installPKc __Z10Rf_protectP7SEXPREC __Z10STRING_ELTP7SEXPRECi __Z10VECTOR_ELTP7SEXPRECi __Z11Rf_isStringP7SEXPREC __Z12Rf_defineVarP7SEXPRECS0_S0_ __Z12Rf_ge>An unusual example of it being easier to debug a shared library problem under Mac OS X ;) -thomas
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
Maybe Matching Threads
- Compiling C++ package source: linking problem?
- RCMD SHLIB == Couldn't reserve space for cygwin's heap, Win32 ?
- RCMD SHLIB == Couldn't reserve space for cygwin's heap, Win32 ?
- g++ 4.3 warning: deprecated conversion from string constant to ‘char*’
- installing knncat package