Faheem Mitha
2005-Jan-30 23:03 UTC
[R] Rinternals.h and iostream don't play nice together'
Hi, Consider the following file. ******************************* foo.cc ******************************* #include <R.h> #include <Rinternals.h> #include <iostream> ******************************* R CMD SHLIB foo.cc gives scads of errors. I've use C++ extensively with R before (using C linkage) but not with Rinternals.h. I'm puzzled. The errors make no sense to me. Am I doing something wrong? Is it impossible to use iostream here for some reason? I'm using Debian Sarge with R 2.0.1 and gcc 3.3.5. Please cc me; I'm not subscribed. Thanks in advance. Faheem. R CMD SHLIB foo.cc g++ -I/usr/lib/R/include -fPIC -g -O2 -c foo.cc -o foo.o ************************************************************************ In file included from /usr/include/c++/3.3/bits/locale_facets.h:528, from /usr/include/c++/3.3/bits/basic_ios.h:44, from /usr/include/c++/3.3/ios:51, from /usr/include/c++/3.3/ostream:45, from /usr/include/c++/3.3/iostream:45, from foo.cc:3: /usr/include/c++/3.3/bits/codecvt.h:110:52: macro "length" passed 4 arguments, but takes just 1 In file included from /usr/include/c++/3.3/bits/locale_facets.h:528, from /usr/include/c++/3.3/bits/basic_ios.h:44, from /usr/include/c++/3.3/ios:51, from /usr/include/c++/3.3/ostream:45, from /usr/include/c++/3.3/iostream:45, from foo.cc:3: /usr/include/c++/3.3/bits/codecvt.h:109: error: parse error before `const' /usr/include/c++/3.3/bits/codecvt.h:113: error: semicolon missing after declaration of `std::__codecvt_abstract_base<_InternT, _ExternT, _StateT>' /usr/include/c++/3.3/bits/codecvt.h:115: error: ISO C++ forbids defining types within return type /usr/include/c++/3.3/bits/codecvt.h:115: error: extraneous `int' ignored /usr/include/c++/3.3/bits/codecvt.h:115: error: non-member function ` std::__codecvt_abstract_base<_InternT, _ExternT, _StateT> std::max_length()' cannot have `const' method qualifier /usr/include/c++/3.3/bits/codecvt.h:115: error: semicolon missing after declaration of `class std::__codecvt_abstract_base<_InternT, _ExternT, _StateT>' /usr/include/c++/3.3/bits/codecvt.h: In function `int std::max_length()': /usr/include/c++/3.3/bits/codecvt.h:115: error: invalid use of `this' in non-member function /usr/include/c++/3.3/bits/codecvt.h: At global scope: /usr/include/c++/3.3/bits/codecvt.h:117: error: parse error before `protected' /usr/include/c++/3.3/bits/codecvt.h:122: error: destructors must be member functions /usr/include/c++/3.3/bits/codecvt.h:122: error: virtual outside class declaration /usr/include/c++/3.3/bits/codecvt.h: In function `void std::__codecvt_abstract_base()': /usr/include/c++/3.3/bits/codecvt.h:122: error: `void std::__codecvt_abstract_base()' redeclared as different kind of symbol /usr/include/c++/3.3/bits/codecvt.h:65: error: previous declaration of ` template<class _InternT, class _ExternT, class _StateT> class std::__codecvt_abstract_base<_InternT,_ExternT,_StateT>' /usr/include/c++/3.3/bits/codecvt.h:65: error: previous non-function declaration `template<class _InternT, class _ExternT, class _StateT> class std::__codecvt_abstract_base<_InternT,_ExternT,_StateT>' /usr/include/c++/3.3/bits/codecvt.h:122: error: conflicts with function declaration `void std::__codecvt_abstract_base()' /usr/include/c++/3.3/bits/codecvt.h: At global scope: /usr/include/c++/3.3/bits/codecvt.h:125: error: syntax error before `(' token /usr/include/c++/3.3/bits/codecvt.h:131: error: syntax error before `(' token /usr/include/c++/3.3/bits/codecvt.h:135: error: syntax error before `(' token /usr/include/c++/3.3/bits/codecvt.h:141: error: virtual outside class declaration /usr/include/c++/3.3/bits/codecvt.h:141: error: non-member function `int std::do_encoding()' cannot have `const' method qualifier /usr/include/c++/3.3/bits/codecvt.h:141: error: function `int std::do_encoding()' is initialized like a variable /usr/include/c++/3.3/bits/codecvt.h:144: error: virtual outside class declaration /usr/include/c++/3.3/bits/codecvt.h:144: error: non-member function `bool std::do_always_noconv()' cannot have `const' method qualifier /usr/include/c++/3.3/bits/codecvt.h:144: error: function `bool std::do_always_noconv()' is initialized like a variable /usr/include/c++/3.3/bits/codecvt.h:147: error: parse error before `&' token /usr/include/c++/3.3/bits/codecvt.h:148: error: virtual outside class declaration /usr/include/c++/3.3/bits/codecvt.h:148: error: non-member function `int std::do_length(...)' cannot have `const' method qualifier /usr/include/c++/3.3/bits/codecvt.h:148: error: function `int std::do_length(...)' is initialized like a variable /usr/include/c++/3.3/bits/codecvt.h:151: error: virtual outside class declaration /usr/include/c++/3.3/bits/codecvt.h:151: error: non-member function `int std::do_max_length()' cannot have `const' method qualifier /usr/include/c++/3.3/bits/codecvt.h:151: error: function `int std::do_max_length()' is initialized like a variable /usr/include/c++/3.3/bits/codecvt.h:158: error: parse error before `<' token /usr/include/c++/3.3/bits/codecvt.h:163: error: syntax error before `;' token /usr/include/c++/3.3/bits/codecvt.h:164: error: syntax error before `;' token /usr/include/c++/3.3/bits/codecvt.h:165: error: syntax error before `;' token /usr/include/c++/3.3/bits/codecvt.h:172: error: only declarations of constructors can be `explicit' /usr/include/c++/3.3/bits/codecvt.h: In function `int codecvt(unsigned int)': /usr/include/c++/3.3/bits/codecvt.h:172: error: `int codecvt(unsigned int)' redeclared as different kind of symbol /usr/include/c++/3.3/bits/codecvt.h:158: error: previous declaration of ` template<class _InternT, class _ExternT, class _StateT> class codecvt' /usr/include/c++/3.3/bits/codecvt.h:158: error: previous non-function declaration `template<class _InternT, class _ExternT, class _StateT> class codecvt' /usr/include/c++/3.3/bits/codecvt.h:172: error: conflicts with function declaration `int codecvt(unsigned int)' /usr/include/c++/3.3/bits/codecvt.h:172: error: only constructors take base initializers /usr/include/c++/3.3/bits/codecvt.h:172: error: parse error before `<' token /usr/include/c++/3.3/bits/codecvt.h:172: confused by earlier errors, bailing out make: *** [foo.o] Error 1
Dirk Eddelbuettel
2005-Jan-30 23:20 UTC
[R] Rinternals.h and iostream don't play nice together'
On 30 January 2005 at 18:03, Faheem Mitha wrote: | Consider the following file. | | ******************************* | foo.cc | ******************************* | #include <R.h> | #include <Rinternals.h> | #include <iostream> | ******************************* | | R CMD SHLIB foo.cc Two changes are required: You absolutely do need extern "C" ... to get C headers in with C++, and the other is a simple reordering -- not sure where I learned that can help at times ... edd at basebud:/tmp> cat foo.cc #include <iostream> extern "C" { #include <R.h> #include <Rinternals.h> } edd at basebud:/tmp> R CMD SHLIB foo.cc g++ -shared -o foo.so foo.o -L/usr/lib/R/lib -lR edd at basebud:/tmp> ls -l foo.so -rwxr-xr-x 1 edd edd 117872 Jan 30 17:16 foo.so edd at basebud:/tmp> Hth, Dirk -- Better to have an approximate answer to the right question than a precise answer to the wrong question. -- John Tukey as quoted by John Chambers
Paul Roebuck
2005-Jan-30 23:24 UTC
[R] Rinternals.h and iostream don't play nice together'
On Sun, 30 Jan 2005, Faheem Mitha wrote:> Consider the following file. > > ******************************* > foo.cc > ******************************* > #include <R.h>and if you use the following instead of "Rinternals.h"? extern "C" { #include <Rdefines.h> }> #include <Rinternals.h> > #include <iostream> > ******************************* > > R CMD SHLIB foo.cc > > gives scads of errors. I've use C++ extensively with R before (using C > linkage) but not with Rinternals.h. I'm puzzled. The errors make no sense > to me. Am I doing something wrong? Is it impossible to use iostream here > for some reason? > [SNIP lots of error messages]'Course I'm assuming you read that part in the 'R-exts.pdf' about using C++ iostreams with R being best avoided? If you really need to do so anyway, I'll give you a link to my iostream manipulator source that interfaces with the R console. It handles the scenario described in section 4.6 of above document but requires the GNU C++ compiler (g++) in order to work though. ---------------------------------------------------------- SIGSIG -- signature too long (core dumped)