Hi, when writing a simple cpp function to be run in R I obtain a compilation error as soon as I include fstream. Any hints of what goes wrong and how to fix it? It's Windows 7 64-bit, R-3.1.2 and Rtools32. An example to recreate the error: #include <R.h> #include <Rdefines.h> #include <fstream> using namespace std; extern "C" SEXP test(SEXP x) { SEXP result; PROTECT(result = allocVector(REALSXP,1)); REAL(result)[0] = REAL(x)[0]*5; UNPROTECT(1); return result; } Output: > R CMD SHLIB test.cpp g++ -m64 -I"C:/PROGRA~1/R/R-31~1.2/include" -DNDEBUG -I"d:/RCompile/CRANpkg/ extralibs64/local/include" -O2 -Wall -mtune=core2 -c test.cpp -o test.o In file included from c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/ ../../../../include/c++/4.6.3/fstream:42:0, from test.cpp:3: c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../include/c+ +/4.6.3/bits/codecvt.h:216:45: error: macro "length" passed 4 arguments, but tak es just 1 In file included from c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/ ../../../../include/c++/4.6.3/fstream:921:0, from test.cpp:3: c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../include/c+ +/4.6.3/bits/fstream.tcc:826:60: error: macro "length" passed 4 arguments, but t akes just 1 c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../include/c+ +/4.6.3/bits/fstream.tcc:943:39: error: macro "length" passed 4 arguments, but t akes just 1 In file included from c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/ ../../../../include/c++/4.6.3/fstream:42:0, from test.cpp:3: c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../include/c+ +/4.6.3/bits/codecvt.h:215:7: error: expected ';' at end of member declaration c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../include/c+ +/4.6.3/bits/codecvt.h:217:7: error: expected unqualified-id before '{' token make: *** [test.o] Error 1 Thanks, sunaga
Try adding the line #define R_NO_REMAP to the top of your file (before any #include's) so the R include files don't define length(x). Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Feb 2, 2015 at 1:11 PM, <sunaga at telenet.be> wrote:> Hi, > > when writing a simple cpp function to be run in R I obtain a compilation > error as soon as I include fstream. Any hints of what goes wrong and how to > fix it? It's Windows 7 64-bit, R-3.1.2 and Rtools32. An example to recreate > the error: > > #include <R.h> > #include <Rdefines.h> > #include <fstream> > using namespace std; > > extern "C" SEXP test(SEXP x) { > SEXP result; > > PROTECT(result = allocVector(REALSXP,1)); > REAL(result)[0] = REAL(x)[0]*5; > > UNPROTECT(1); > return result; > } > > Output: > > > R CMD SHLIB test.cpp > g++ -m64 -I"C:/PROGRA~1/R/R-31~1.2/include" -DNDEBUG > -I"d:/RCompile/CRANpkg/ > extralibs64/local/include" -O2 -Wall -mtune=core2 -c test.cpp -o > test.o > In file included from > c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/ > ../../../../include/c++/4.6.3/fstream:42:0, > from test.cpp:3: > > c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../include/c+ > +/4.6.3/bits/codecvt.h:216:45: error: macro "length" passed 4 > arguments, but tak > es just 1 > In file included from > c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/ > ../../../../include/c++/4.6.3/fstream:921:0, > from test.cpp:3: > > c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../include/c+ > +/4.6.3/bits/fstream.tcc:826:60: error: macro "length" passed 4 > arguments, but t > akes just 1 > > c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../include/c+ > +/4.6.3/bits/fstream.tcc:943:39: error: macro "length" passed 4 > arguments, but t > akes just 1 > In file included from > c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/ > ../../../../include/c++/4.6.3/fstream:42:0, > from test.cpp:3: > > c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../include/c+ > +/4.6.3/bits/codecvt.h:215:7: error: expected ';' at end of member > declaration > > c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../include/c+ > +/4.6.3/bits/codecvt.h:217:7: error: expected unqualified-id before > '{' token > make: *** [test.o] Error 1 > > Thanks, > sunaga > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Thank you, it worked. For completeness, had to add Rf_ in front of allocVector (and possibly any other function from Rdefines.h) ----- Original Message ----- From: "William Dunlap" <wdunlap at tibco.com> To: sunaga at telenet.be Cc: r-help at r-project.org Sent: Monday, February 2, 2015 10:29:19 PM Subject: Re: [R] rtools fstream error Try adding the line #define R_NO_REMAP to the top of your file (before any #include's) so the R include files don't define length(x). Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Feb 2, 2015 at 1:11 PM, < sunaga at telenet.be > wrote: Hi, when writing a simple cpp function to be run in R I obtain a compilation error as soon as I include fstream. Any hints of what goes wrong and how to fix it? It's Windows 7 64-bit, R-3.1.2 and Rtools32. An example to recreate the error: #include <R.h> #include <Rdefines.h> #include <fstream> using namespace std; extern "C" SEXP test(SEXP x) { SEXP result; PROTECT(result = allocVector(REALSXP,1)); REAL(result)[0] = REAL(x)[0]*5; UNPROTECT(1); return result; } Output:> R CMD SHLIB test.cppg++ -m64 -I"C:/PROGRA~1/R/R-31~1.2/include" -DNDEBUG -I"d:/RCompile/CRANpkg/ extralibs64/local/include" -O2 -Wall -mtune=core2 -c test.cpp -o test.o In file included from c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/ ../../../../include/c++/4.6.3/fstream:42:0, from test.cpp:3: c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../include/c+ +/4.6.3/bits/codecvt.h:216:45: error: macro "length" passed 4 arguments, but tak es just 1 In file included from c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/ ../../../../include/c++/4.6.3/fstream:921:0, from test.cpp:3: c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../include/c+ +/4.6.3/bits/fstream.tcc:826:60: error: macro "length" passed 4 arguments, but t akes just 1 c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../include/c+ +/4.6.3/bits/fstream.tcc:943:39: error: macro "length" passed 4 arguments, but t akes just 1 In file included from c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/ ../../../../include/c++/4.6.3/fstream:42:0, from test.cpp:3: c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../include/c+ +/4.6.3/bits/codecvt.h:215:7: error: expected ';' at end of member declaration c:\rtools\gcc-4.6.3\bin\../lib/gcc/i686-w64-mingw32/4.6.3/../../../../include/c+ +/4.6.3/bits/codecvt.h:217:7: error: expected unqualified-id before '{' token make: *** [test.o] Error 1 Thanks, sunaga ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]