@@ -795,6 +795,8 @@ namespace attributes {
795795
796796 std::string generateRArgList (const Function& function);
797797
798+ void initializeGlobals (std::ostream& ostr);
799+
798800 void generateCpp (std::ostream& ostr,
799801 const SourceFileAttributes& attributes,
800802 bool includePrototype,
@@ -2127,9 +2129,8 @@ namespace attributes {
21272129
21282130 // always bring in Rcpp
21292131 ostr << " using namespace Rcpp;" << std::endl << std::endl;
2130- ostr << " Rostream<true> &Rcpp::Rcout = Rcpp_cout_get();" << std::endl;
2131- ostr << " Rostream<false> &Rcpp::Rcerr = Rcpp_cerr_get();" << std::endl;
2132- ostr << std::endl;
2132+ // initialize references to global Rostreams
2133+ initializeGlobals (ostr);
21332134
21342135 // commit with preamble
21352136 return ExportsGenerator::commit (ostr.str ());
@@ -2745,6 +2746,16 @@ namespace attributes {
27452746 return argsOstr.str ();
27462747 }
27472748
2749+ // Generate the C++ code required to initialize global objects
2750+ void initializeGlobals (std::ostream& ostr) {
2751+ ostr << " #ifdef RCPP_USE_GLOBAL_ROSTREAM" << std::endl;
2752+ ostr << " Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();" ;
2753+ ostr << std::endl;
2754+ ostr << " Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();" ;
2755+ ostr << std::endl;
2756+ ostr << " #endif" << std::endl << std::endl;
2757+ }
2758+
27482759 // Generate the C++ code required to make [[Rcpp::export]] functions
27492760 // available as C symbols with SEXP parameters and return
27502761 void generateCpp (std::ostream& ostr,
@@ -3191,6 +3202,8 @@ namespace {
31913202 // always include Rcpp.h in case the user didn't
31923203 ostr << std::endl << std::endl;
31933204 ostr << " #include <Rcpp.h>" << std::endl;
3205+ // initialize references to global Rostreams
3206+ initializeGlobals (ostr);
31943207 generateCpp (ostr, sourceAttributes, true , false , contextId_);
31953208 generatedCpp_ = ostr.str ();
31963209 std::ofstream cppOfs (generatedCppSourcePath ().c_str (),
0 commit comments