ShanZhengYang ShanZhengYang - 19 days ago 5
C++ Question

Rcpp: porting C++ function into R, 'Rcpp.h' file not found

I have some C++ code. I would like to make this an R package with Rcpp.

Here's what I'm doing:


  1. Create a package framework by executing
    Rcpp.package.skeleton("myPackageName")

  2. Run
    devtools::load_all()
    to see whether
    rcpp_hello_world()
    works after
    library(myPackageName)
    . It does!

  3. Take all C++ source code and copy this into
    /src
    . I can still use
    rcpp_hello_world()
    after running
    devtools::load_all()
    once again.

  4. For each function in the C++ source code now residing in
    /src
    , simply do this:



(A) Put at the top of the
.cpp
file `

#include <Rcpp.h>
using namespace Rcpp;


(B) Put before each function
void
this comment

//[[Rcpp::export]]


Now, I foolishly think that if I run
devtools::load_all()
, the code with compile and those C++ functions will be accessible. This is incorrect.

Here's the error after
load_all()
:

1 warning generated.
sourcecode1.cc:2:10: fatal error: 'Rcpp.h' file not found
#include <Rcpp.h>
^
1 error generated.
make: *** [myPackageName] Error 1
ERROR: compilation failed for package ‘myPackageName’


It cannot find the header file
Rcpp.h


(1) Is my procedure above incorrect in creating R packages directly from C++ code with Rcpp? This is what I've gathered from the book

(2) How can I solve this problem?

Answer

You need to run 'compileAttributes()' which RStudio would do for you too.

In short each time you alter or add interfaces, you need to run this function.