简体   繁体   中英

R check doesn't like std:cout (C++)

I'm trying to submit a package to CRAN which contains C++ code (I have no clue about C++, the cpp files were written by somebody else).

The R check complains about 'std::cout' (C++) Compiled code should not call entry points which might terminate R nor write to stdout/stderr instead of to the console, nor the C RNG

I found in the code the following command:

 integrate_const(stepper_type( default_error_checker< double >( abs_error , rel_error ) ),
                mDifEqn,
                x,
                0.0,
                (precipitationLength * timeStep), 
                timeStep,
                streaming_observer(std::cout) ); 

I guess R (CRAN) expects something else rather than std::cout... but what?

Your C++ project may well be using standard input and output.

The issue, as discussed in the Writing R Extensions manual, is that you then end up mixing two output systems: R's, and the C++ one.

So you are "encouraged" to replace all uses of, say,

 std::cout << "The value of foo is " << foo << std::endl;

with something like

 Rprintf("The value of foo is %f\n", foo);

so that your output gets blended properly with R's. In one of my (non-Rcpp) packages I had to do a lot of tedious patching for that...

Now, as mentioned in a comment by @vasicbre and an answer by @Dason, if you use Rcpp you can simply do

 Rcpp::Rcout << "The value of foo is " << foo << std::endl;

If you already use Rcpp this is pretty easy, otherwise you need to decide if that makes it worth adding Rcpp...

edit : fixed typo in Rcpp::Rcout .

If you want to stream to R's buffered output you'll want to use Rcpp::Rcout instead of std::cout.

For more details you can read this article by one of Rcpp's authors: http://dirk.eddelbuettel.com/blog/2012/02/18/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM