1

I have a package that uses Rcpp. It has different C++ function defined int src/testr.h. Now at some point I need to generate Rcpp functions (using cppFunction or cxxfunction) that use things defined in src/testr.h. How can I do that?

I tried,

> cxxfunction(signature(x="list") includes = c('#include "testr.h"'), body=sprintf(template, name, name))
> Rcpp::cppFunction(depends = "testr", includes = c('#include "src/testr.h"'), sprintf(template, name, name))

But

file7086270f0da.cpp:7:10: fatal error: 'testr.h' file not found
#include "testr.h"

     ^      

1 Answer 1

4

Quick suggestion:

  1. Move the header to inst/include/testr.h;
  2. This requires a src/Makevars entry such as PKG_CPPFLAGS = -I../inst/include to have the compilation look there.
  3. Now with the package installed as, say, mypkg, you can add a depends="mypkg" and R will know to set a -I... for you.

We use the same trick all the time in e.g. BH and various Rcpp* packages.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for a suggestion. I moved the header as you proposed, but I guess because the implementation of a functions from header is still in src/testr.cpp, I get following errors like Symbol not found: __Z7deparseP7SEXPREC. @dirk-eddelbuettel, do you know how can that be fixed?
What I suggested assumed a header-only approach (as that was also what the subject of your question suggested). When you want to import functions from another package and need linking, then a more involved approach is needed.
The "Writing R Extensions" manual that came with R. No easy Rcpp wrapper, but you can eg look at how RApiSerialize provides a function for RcppRedis, or how xts provides some functions for RcppXts. By far the easiest is to convert your code to be header-only if you can.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.