[Rcpp-devel] How to handle std::cout/std::cerr in shared libraries

Watal M. Iwasaki heavy.watal at gmail.com
Fri Oct 26 09:31:13 CEST 2018


Hi all,

I have been working on an R interface package to a shared library written
in C++. It works almost perfectly, but the output to std::cout is not shown
on R console (on Linux). Of course Rcpp::Rcout should be used instead of
std::cout. But in this case I want to let the shared library be pure C++.
So I tried switching the stream buffers in the Rcpp side:
```
Rcpp::CharacterVector run(const std::vector<std::string>& args) {
    std::streambuf* obuf = std::cout.rdbuf(Rcpp::Rcout.rdbuf());
    mylib::Simulation simulation(args);
    simulation.run(); // std::cout is used in here
    std::cout.rdbuf(obuf);
    return "something";
}
```

It (seemingly) works! But now `devtools::check()` produces 1 note:
```
❯ checking compiled code ... NOTE
  File ‘mylib/libs/mylib.so’:
    Found ‘__ZNSt3__14coutE’, possibly from ‘std::cout’ (C++)
      Object: ‘run.o’

  Compiled code should not call entry points which might terminate R nor
  write to stdout/stderr instead of to the console, nor use Fortran I/O
  nor system RNGs.
```

Can I suppress this note? or should I just ignore it? any other good
solution?

Thanks,
Watal M. Iwasaki
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20181026/edd5272c/attachment.html>


More information about the Rcpp-devel mailing list