2

I am trying to play with the Rcpp package from the R statistical software. Unfortunately, I cannot compile a basic example with the sourceCpp function. My toy example is the following:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
int hello(int a)
{
    return a;
}

Saved into the file "helloworldR.cpp.

Then when I call sourceCpp:

library(Rcpp)
sourceCpp("helloworldR.cpp")

I get the following error:

In file included from /home/userD/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include/RcppCommon.h:65:0,
                  from /home/master/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include/Rcpp.h:27,
                  from helloworldR.cpp:1:
 /home/userD/map:1:1: error: stray ‘\177’ in program
  ^?ELF^B^A^A         ^B > ^A    ^K@     @       Xw          @ 8   @   ^] ^F   ^E
  ^
 /home/userD/map:1:8: warning: null character(s) ignored

make: *** [helloworldR.o] Error 1
g++  -I/usr/local/lib/R/include -DNDEBUG   -I"/home/master/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include" -I"/home/userD" -I/usr/local/include   -fpic  -g -O2  -c helloworldR.cpp -o helloworldR.o
 /usr/local/lib/R/etc/Makeconf:168: recipe for target 'helloworldR.o' failed
 Error in sourceCpp("helloworldR.cpp") :
   Error 1 occurred building shared library.

But if I put the code as a string it works:

sourceCpp(code = "#include <Rcpp.h>
 + using namespace Rcpp;
 +
 + // [[Rcpp::export]]
 + int hello(int a)
 + {
 +   return a;
 + }
 + ")
 > hello(2)
 [1] 2

What do you think it can be? Here is my session information:

sessionInfo()
 R version 3.4.3 (2017-11-30)
 Platform: x86_64-pc-linux-gnu (64-bit)
 Running under: Ubuntu 16.04.3 LTS

 Matrix products: default
 BLAS: /usr/local/lib/R/lib/libRblas.so
 LAPACK: /usr/local/lib/R/lib/libRlapack.so

 locale:
  [1] LC_CTYPE=ca_ES.UTF-8       LC_NUMERIC=C
  [3] LC_TIME=ca_ES.UTF-8        LC_COLLATE=ca_ES.UTF-8
  [5] LC_MONETARY=ca_ES.UTF-8    LC_MESSAGES=ca_ES.UTF-8
  [7] LC_PAPER=ca_ES.UTF-8       LC_NAME=C
  [9] LC_ADDRESS=C               LC_TELEPHONE=C
 [11] LC_MEASUREMENT=ca_ES.UTF-8 LC_IDENTIFICATION=C

 attached base packages:
 [1] stats     graphics  grDevices utils     datasets  methods   base

 other attached packages:
 [1] ggplot2_2.2.1 Rcpp_0.12.15

 loaded via a namespace (and not attached):
  [1] colorspace_1.3-2 scales_0.5.0     compiler_3.4.3   lazyeval_0.2.1
  [5] plyr_1.8.4       tools_3.4.3      pillar_1.1.0     gtable_0.2.0
  [9] tibble_1.4.2     grid_3.4.3       rlang_0.1.6      munsell_0.4.3
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Oriol
  • 56
  • 5
  • 1
    Please tell us, what *is* "Rcpp"? – Jesper Juhl Jan 31 '18 at 17:16
  • 1
    It was wrongly spelled, it is an R package to combine C++ and R code – Oriol Jan 31 '18 at 17:22
  • 1
    @JesperJuhl Click on the tag and see the info--it is an R extension package which greatly facilitates C++ integration into R. – Dirk Eddelbuettel Jan 31 '18 at 17:24
  • Also a stray error and Rcpp (but probably unrelated): *[Rcpp Compiler error in r tools for Visual Studio 2015](https://stackoverflow.com/questions/52566312/)* – Peter Mortensen May 06 '23 at 21:10
  • "[`ELF`](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format)" is a hint the compiler is chewing on binary data for some reason (\177 is not in the source code, though [in other cases it may be](https://stackoverflow.com/questions/45530960/g-error-stray-177-in-program)). It is often the result of some build system/make shenanigans. – Peter Mortensen May 06 '23 at 21:22
  • Here is another example with ELF and \177: *[Why do I get weird stray errors with file 'cxxopts.hpp' and Meson + Ninja build?](https://stackoverflow.com/questions/73410284/)* – Peter Mortensen May 06 '23 at 21:27
  • Here is another example where the compiler is chewing on binary data (with an explanation of exactly how in the answer): *[Daemon on embedded Linux device using BusyBox be written in C or as a script](https://stackoverflow.com/questions/28759855/)* – Peter Mortensen May 06 '23 at 21:29
  • Indeed, the very first byte of an ELF file is 0x7F (octal 177, decimal 127). – Peter Mortensen May 07 '23 at 00:12

1 Answers1

0

You may be editing your source with an inappropriate application leaving odd things behind:

/home/userD/map:1:1: error: stray ‘\177’ in program

Take care of that first, and things will improve.

You can also greatly help yourself if you use one of (at least two) generators for working examples:

  1. Call Rcpp.package.skeleton("mypackagenamehere")
  2. Use RStudio and do File -> New Project -> New Directory after which you can select Package / Package with Rcpp.

Build the generated example. Rejoice that it works. Then modify it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Using Rcpp.package skeleton I am able to compile and call C++ functions in R, so it works, thank you. – Oriol Feb 01 '18 at 11:14